import java.util.*;
public class Student implements Comparator,Comparable{
private String name;
pr
import java.util.*;
public class Student implements Comparator,Comparable{
private String name;
private int age;
private double score;
Student(String name,int age,double score)
{
this.name=name;
this.age=age;
this.score=score;
}
public int compareTo(Student s){
int num = new Double(this.score).compareTo(new Double(s.score));
if(num==0)
return this.name.compareTo(s.name);
return num;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getScore()
{
return score;
}
public void setName(String name)
{
this.name =name;
}
public void setAge(int age)
{
this.age=age;
}
public void setScore(double score)
{
this.score=score;
}
public int compare(Student s1, Student s2) {
// TODO Auto-generated method stub
int num = s1.getScore().compareTo(s2.getScore());
if(num==0)
return new Double(s1.getScore()).compareTo(new Double(s2.getScore()));
return num;
}
public static void main(String[] args){
TreeSet ts = new TreeSet();
ts.add(new Student("zhangsan",20,85.5));
ts.add(new Student("lisi",21,82.5));
ts.add(new Student("wnagwu",25,90.0));
ts.add(new Student("zhaoliu",22,88.5));
ts.add(new Student("biqi",22,95.0));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu =(Student)it.next();
System.out.println(stu.getName()+"....."+stu.getAge()+"..."+stu.getScore());
}
}
}
这段代码报错怎么解决?
展开
全部