public class Student {
private String name;
private int age;
public Student(){}
public Student(String name , int age){
this.name = name;
this.age = age;
}
//重写toString方法
public String toString(){
return name+" " + age;
}
//=============GS===============================================
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
mport java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String[] args){
ArrayList<Student> student = new ArrayList<Student>();
Student s1 = new Student("xiaoming",20);
Student s2 = new Student("xiaohong",20);
student.add(s1);
student.add(s2);
//返回迭代器
Iterator<Student> iterator = student.iterator();
//调用hasNext与next完成集合迭代
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println(s);
}
}
} |
|