本帖最后由 小陈期待逆袭 于 2013-5-16 15:33 编辑
我写了一段代码,给五个学生按照分数排序,编译可以通过,运行时,一直在等待。。求大神帮忙分析一下
public class Test10 {
/**
* @param args
*/
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new MyComparator());
ts.add(new Student("Jim",16,98.2));
ts.add(new Student("Tina",16,100.0));
ts.add(new Student("Tom",17,93.5));
ts.add(new Student("Jack",15,88.6));
ts.add(new Student("Jay",16,98.2));
Iterator<Student> it=ts.iterator();
while(it.hasNext());
{
Student s=it.next();
System.out.println("name:"+s.getName()+" age:"+s.getAge()+" Score:"+s.getScore());
}
}
}
class Student
{
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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
class MyComparator implements Comparator<Student>
{
public int compare(Student s1, Student s2) {
if( s1.getScore() >(s2.getScore()))
return 1;
if(s1.getScore()==s2.getScore())
return 0;
return -1;
}
} |