package package4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class StudentTest {
public static void main(String[] args) {
//创建集合对象
Collection<Student> c = new ArrayList<>();
//创建学生对象
Student s1 =new Student("万青",21);
Student s2 =new Student("霍胜",22);
Student s3 =new Student("万松",23);
//添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
//遍历集合(迭代器方式)
Iterator<Student> itr =c.iterator();
while (itr.hasNext()){
// 这种形式是不行的,因为Name的next是第一个人,
// 后面Age的itr.next就指向了下一个人,不匹配
//System.out.println( itr.next().getName()+“,”+itr.next().getAge());
Student s = itr.next();
System.out.println(s.getName()+", "+s.getAge());
}
}
}
标签:遍历,Iterator,Collection,next,Student,new,itr
From: https://www.cnblogs.com/knlf/p/16793960.html