Iterator it = ts.iterator();
while(it.hasNext()){
Students stu = (Students)it.next();
sop(stu.getName()+" "+stu.getAge());
}
}
}
class Students implements Comparable{ //该接口强制让学生具备比较性
private String name;
private int age;
Students(String name, int age){
this.name = name;
this.age = age;
}
public int compareTo(Object obj){ if(!(obj instanceof Students)) //为啥要写这句 throw new RuntimeException("不是学生对象");
Students s = (Students)obj;
if(this.age - s.age == 0){
return this.name.compareTo(s.name);
}
else
return this.age - s.age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}