Java代码如下:
package Test01; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Test06 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub a(); b(); } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void a(){ List list = new ArrayList(); for (int i = 0; i < 10; i++) { list.add(i); System.out.print(list.get(i)+" "); } System.out.println(); Map map = new HashMap(); for (int i = 0; i < list.size(); i++) { map.put(i, list.get(i)); System.out.print(map.get(i)); } System.out.println(); System.out.println(map); } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void b(){ List list = new ArrayList(); list.add("河南"); list.add("郑州"); list.add("登封"); list.add("开封"); list.add("上海"); System.out.print(list); System.out.println(); Map map = new HashMap(); for (int i = 0; i < list.size(); i++) { map.put(i, list.get(i)); System.out.print(map.get(i)); } System.out.println(); System.out.println(map); } }
代码运行后效果如下: