package Chapter1;
import java.util.ArrayList;
import java.util.Iterator;
public class Student {
private String name;
private int age;
public Student(){
super();
}
public Student(String name,int age){
super();
this.name=name;
this.age=age;
}
public void setName(){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(){
this.age=age;
}
public int getAge(){
return age;
}
public static void main(String[] args){
ArrayList<Student> li=new ArrayList<Student> ();
Student t1=new Student("Lily",22);
Student t2=new Student("Jessie",21);
Student t3=new Student("Oscar",25);
Student t4 = new Student("Peggie",16);
li.add(t1);
li.add(t2);
li.add(t3);
li.add(t4);//add方法添加对象之后是按照算法随机放的位置,用单纯的循环是不能遍历的
Iterator<Student> itr=li.iterator();
while(itr.hasNext()){
Student result=itr.next();
System.out.println(result.getName()+result.getAge());//若直接打印itr.next(),输出的是对象,而不是名字和年龄。
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class Student {
private String name;
private int age;
public Student(){
super();
}
public Student(String name,int age){
super();
this.name=name;
this.age=age;
}
public void setName(){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(){
this.age=age;
}
public int getAge(){
return age;
}
public static void main(String[] args){
ArrayList<Student> li=new ArrayList<Student> ();
Student t1=new Student("Lily",22);
Student t2=new Student("Jessie",21);
Student t3=new Student("Oscar",25);
Student t4 = new Student("Peggie",16);
li.add(t1);
li.add(t2);
li.add(t3);
li.add(t4);//add方法添加对象之后是按照算法随机放的位置,用单纯的循环是不能遍历的
Iterator<Student> itr=li.iterator();
while(itr.hasNext()){
Student result=itr.next();
System.out.println(result.getName()+result.getAge());//若直接打印itr.next(),输出的是对象,而不是名字和年龄。
}
}
}