student类:
public class student {
private String name;
private int age;
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;
}
public student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public student() {
super();
}
@Override
public String toString() {
return "student [name=" + name + ", age=" + age + "]";
}
}
测试类:
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建集合对象
List list=new ArrayList();
student s1=new student("a",23);
student s2=new student("b",24);
student s3=new student("c",25);
list.add(s1);
list.add(s2);
list.add(s3);
//迭代器遍历
Iterator it=list.iterator();
while(it.hasNext()){
student s=(student) it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
System.out.println("-----------------");
//for循环
for(int i=0;i<list.size();i++){
student s=(student) list.get(i);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}