import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Test14 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Student> set=new TreeSet<Student>();
set.add(new Student("tang",70));
set.add(new Student("li",75));
set.add(new Student("wang",74));
set.add(new Student("wu",73));
Iterator<Student> it=set.iterator();
while(it.hasNext())
{
Student stu=it.next();
System.out.println(stu.getName()+" "+stu.getSum());
}
//
// for(Student stu:set)
// {
// System.out.println(stu.getName()+" "+stu.getSum());
// }
}
}
class Student implements Comparable<Student> {
private String name;
private int sum;
public Student() {
super();
}
public Student(String name, int sum) {
this.name = name;
this.sum = sum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSum()
{
return sum;
}
public void setSum(int sum)
{
this.sum=sum;
}
@Override
public int compareTo(Student stu) {
// TODO Auto-generated method stub
int tmp = this.sum - this.sum;
return tmp == 0 ? this.name.compareTo(this.name) : tmp;
}
}
为什么我打印出只有一个数据。。那个地方出错了。。谢谢
|
|