import java.util.*;
class Test
{
public static void diedai(Collection c)
{
Iterator i=c.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
public static void main(String[] args)
{
Student s1=new Student(2,"zhangsan");
Student s2=new Student(3,"lisi");
Student s3=new Student(1,"wangwu");
ArrayList<Object> a=new ArrayList<Object>();
a.add(s1);
a.add(s2);
a.add(s3);
Collections.sort(a);
diedai(a);
}
}
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;
}
}
运行时出现出现了Collections.sort(a)找不到符号
把ArrayList<Object>a1=new ArrayList<Object>();
这句换成
ArrayList<Student>a1=new ArrayList<Student>();就出现了使用了未经检查或不安全的操作
|