又出错了!?
import java.util.*;
class MyArray
{
public static void main(String [] args)
{
ArrayList <Student> al=new ArrayList <Student> ();
al.add(new Student(2, "zhangsan "));
al.add(new Student(1, "lisi "));
al.add(new Student(3, "wangwu "));
Collections.sort(al);
Iterator it=al.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
class Student implements Comparable
{
int num;
String name;
public 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?-1:0;
}
public String toString()
{
return num+ "---------- "+name;
}
}
我用的是JDK1.6
报错信息为:
D:\jdk> javac MyArray.java
注意:MyArray.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。
应该是泛型的问题!怎么解决啊!盼朋友解决一下;
------解决方案--------------------已经说得很清楚了。你这样就行了:
javac MyArray.java -Xlint:unchecked
------解决方案--------------------class Student implements Comparable
改为:
class Student implements Comparable <Object>