Collections.sort()调用错误
import java.lang.*;import java.util.*;
class ArrayListTest
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
Student s1=new Student(2,"zhangsan");
Student s2=new Student(1,"lisi");
Student s3=new Student(3,"wangwu");
ArrayList<Object> al=new ArrayList<Object>();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
printElements(al);
}
}
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="+num+","+"name="+name;
}
}
为什么编译错误啊:
说是找不到符号,好想是sort()方法找不到啊。(我的是JDK1.5)