当前位置: 代码迷 >> J2SE >> 求帮忙解决一个有关问题
  详细解决方案

求帮忙解决一个有关问题

热度:101   发布时间:2016-04-23 19:48:43.0
求帮忙解决一个问题
import java.util.*;
public class TreeSetDemo {

public static void main(String[] args) {
TreeSet tr = new TreeSet();

tr.add(new Student("夏",20));
tr.add(new Student("费",21));
tr.add(new Student("童",30));
tr.add(new Student("陈",25));

Iterator it = tr.iterator();
while(it.hasNext()){
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}


}

}
 class Student implements Comparable
{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public int comparaTo(Object obj){
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s=(Student)obj;

System.out.println(this.name+".....compareto......"+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
return 0;
return -1;
}

public String getName(){
return name;
}
public int getAge(){
return age;
}
}
------解决思路----------------------

@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}


非 
public int comparaTo(Object obj){

拼写错误
  相关解决方案