如List集合:
List list = new ArrayList();
list.add(new Student("张三", 23));
list.add(new Student("李四", 24));
list.add(new Student("王五", 25));
list.add(new Student("赵六", 26));
遍历方式
1.转成数组遍历
for(int i = 0;i < list.size();i++){
Student s = (Student)list.get(i);
System.out.println(s.getName() + "..." + s.getAge());
}
2.迭代器遍历
iterator it = list.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName() + "..." + s.getAge());
}
3.高级for遍历
for(Object obj : list){
Student s = (Student)obj;
System.out.println(s.getName() + "..." + s.getAge());
} |
|