listjava
Student s1=new Student("張三",1); Student s2=new Student("李四",2); Student s3=new Student("王五",2); List<Student> list=new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); for(int i=0;i<list.size();i++) { System.out.println(list.get(i).getName()); }
map 遍歷注意Entry()spa
HashMap<Integer,String> map=new HashMap(); map.put(1, "aaa"); map.put(2, "bbb"); map.put(3, "ccc"); //map.remove(3); // for(Map.Entry<Integer, String> entry : map.entrySet())//遍歷反法1 // { // System.out.println(entry.getKey()+entry.getValue()); // } //遍歷方法2 for(Integer key :map.keySet()) { System.out.println(key); } for(String val :map.values()) { System.out.println(val); } //遍歷方法3 Iterator<Map.Entry<Integer, String>> it=map.entrySet().iterator(); while(it.hasNext()) { Map.Entry<Integer, String> ma=it.next(); System.out.println(ma.getKey()); System.out.println(ma.getValue()); } }