import java.util.*;
class Student implements Comparable
{
int id;
String name;
public Student(int id, String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return id + " " +name ;
}
public int compareTo(Object o)
{
Student st = (Student)o;
if(this.id == st.id)
{
return 0;
}
else if(this.id < st.id)
{
return -1;
}
else
{
return 1;
}
}
}
public class Test1
{
public static void main(String args[])
{
LinkedList L = new LinkedList();
L.add(new Student(1, "张三"));
L.add(new Student(10, "李大"));
L.add(new Student(2, "李四"));
L.add(new Student(3, "王五"));
Collections.sort(L);
System.out.println(L);
}
}
以上代码,我用cmd窗口javac编译时,会显示:
错误:此处需要接口。
但是用eclipse编译成功。
不知道是为什么。有人能解答下么?
请问你jdk的版本是多少。
我在jdk1.6中用你的代码做了一下测试,可以执行出结果,只是在编译的时候报"使用了未经检查或不安全的操作......",这个是在声明集合的时候没有指定泛型造成的,你可以在声明集合的时候指定泛型,然后再看还报不报错LinkedList L = new LinkedList();
如果你能用eclipse编译成功,确认这一点,是真正的成功,那么应该是你调用javac的命令行有问题。
因为错误的代码肯定不能编译成功,但是正确的代码可能编译不成功。