import java.util.*;
class ArrayListTest{
public static void printElements(Collection d){
Iterator it =d.iterator();
while(it.hasNext()){
System.out.println(it);
}
}
public static void main(String[] args){
Student s1=new Student(1,"zhangsan");
Student s2=new Student(4,"lisi");
Student s3=new Student(3,"wangwu");
ArrayList x=new ArrayList();
x.add(s1);
x.add(s2);
x.add(s3);
Collections.sort(x);
printElements(x);
}
}
class Student implements Comparable{
int num;
String name;
Student(int num,String name){
this.num=num;
this.name=name;
}
public int compareTo(Object o){
Student s=(Student)o;
return num>s.num ? 1: (num==s.num ? 0 : -1);
}
public String toString(){
return num+":"+name;
}
}
报一个注意。为什么?