package com.smic.test; public class Student { private String name = null; private Integer age = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, Integer age) { super(); this.name = name; this.age = age; } }
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); List<Student> l=new ArrayList<Student>(); l.add(s1); l.add(s2); l.add(s3);
for (int i = 0; i < l.size(); i++) { System.out.println(l.get(i).getName()); }
for (Student stu : l) { System.out.println(stu.getName()+"\t"+stu.getAge()); }
Iterator<Student> is=l.iterator(); while(is.hasNext()){ Student s=is.next(); //错误书写,只能用一次is.next()迭代器 // System.out.println(is.next().getName()+"\t"+is.next().getAge()); System.out.println(s.getName()+"\t"+s.getAge()); }
Iterator i=l.iterator() while(i.hasNext()){ Student s=(Student)i.next();//强转 System.out.println(s.getName()+"\t"+s.getAge()); }
for(Iterator i=l.iterator();i.hasNext();){ Student s=(Student)i.next(); System.out.println(s.getName()+"\t"+s.getAge()); }
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); Set<Student> s=new HashSet<Student>(); s.add(s1); s.add(s2); s.add(s3);
Iterator<Student> is=s.iterator(); while(is.hasNext()){ Student stu=is.next(); System.out.println(stu.getName()+"\t"+stu.getAge()); }
for (Student stu : s) { System.out.println(stu.getName()+"\t"+stu.getAge()); }
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); Map<String,Student> ms=new HashMap<String,Student>(); ms.put("stu1", s1); ms.put("stu2", s2); ms.put("stu3", s3);
Iterator<Map.Entry<String, Student>> it=ms.entrySet().iterator();//Map.可以不写 while(it.hasNext()){ Map.Entry<String, Student> stu=it.next(); System.out.println(stu.getKey()+"\t"+stu.getValue().getName()); }
Set<String> set=ms.keySet(); for (String str : set) { System.out.println(str+"\t"+ms.get(str).getName()); }
for(Entry<String, Student>entry:ms.entrySet()){ System.out.println(entry.getKey()+"\t"+entry.getValue().getName()); }
Collection<Student> col=ms.values(); for (Student stu : col) { System.out.println(stu.getName()); }
Student s1=new Student("卫庄", 32); Student s2=new Student("盖聂", 35); Student s3=new Student("黑白玄翦", 36); Map<String,Student> ms=new HashMap<String,Student>(); ms.put("stu1", s1); ms.put("stu2", s2); ms.put("stu3", s3); List<Map<String,Student>> list=new ArrayList<Map<String,Student>>(); list.add(ms);
------------------------------------------------------------------------------------------------------------------
for (int i = 0; i < list.size(); i++) { Map<String,Student> map=list.get(i); Set<String> set=map.keySet(); for (String str : set) { System.out.println(str+"\t"+map.get(str).getName()); } }
------------------------------------------------------或者等等-------------------------------------------------
for (int i = 0; i < list.size(); i++) { Map<String,Student> map=list.get(i); for(Entry<String, Student> entry:map.entrySet()){ System.out.println(entry.getKey()+"\t"+entry.getValue().getName()); } }
原文:https://www.cnblogs.com/xiaohu666/p/10023340.html