import java.util.Set ;
import java.util.TreeSet ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String gtoString(){
return "姓名:" + this.name + ";年龄:" + this.age ;
}
};
public class TreeSetDemo{
public static void main(String args[]){
Set<Person> allSet = new TreeSet<Person>() ;
allSet.add(new Person("张三",30)) ;
allSet.add(new Person("李四",31)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("赵六",33)) ;
allSet.add(new Person("孙七",33)) ;
System.out.println(allSet) ;
}
};
抛了这个异常:
Exception in thread "main" java.lang.ClassCastException: cn.dan.wenti.Person cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
at java.util.TreeSet.add(TreeSet.java:238)
at cn.dan.wenti.Test7.main(TreeSetDemo.java:20)
说没有实现Comparable接口,谁能帮我讲下吗怎么回事吗? |