import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /* * list是對象集合,容許對象重複。map是鍵值對的集合,不容許key重複。 * list的輸入方法和輸出方法 * map的輸入方法和輸出方法 * 利用迭代器將list裏的值取出來 * */ public class ListTest { public static void main(String[] args) { List<Object> list=new ArrayList<Object>(); list.add("dd"); list.add("aa"); list.add("cc"); for(Iterator<Object> iterator=list.iterator();iterator.hasNext();) { Object string=iterator.next(); System.out.println(string); } Map<String, String> map0=new HashMap<String, String>(); map0.put("name", "zhangsan"); map0.put("***", "female"); String nameString=map0.get("name"); String ***String=map0.get("***"); System.out.println(nameString "\n" ***String); List<Map<String, Object>> lists=new ArrayList<Map<String,Object>>(); Map<String, Object> map=new HashMap<String, Object>(); map.put("name", "ddd"); map.put("age", 23); Map<String, Object> map1=new HashMap<String, Object>(); map1.put("name", "ccc"); map1.put("age", 43); lists.add(map); lists.add(map1); Iterator<Map<String, Object>> it=lists.iterator(); for(;it.hasNext();) { Map<String, Object> map2 =it.next(); String name=(String) map2.get("name"); Object age= map2.get("age"); System.out.println("name=" name "\nage=" age); } } }