您的位置:首页 > 职场人生

黑马程序员---------------------声明类Students,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,按照成绩排序输出结果(考虑成绩相同的问题)。

2013-12-25 18:08 585 查看
----------------------ASP.Net+Android+IOS开发、.Net培训、期待与您交流! --------------------  

public>public>TreeSet<Students>>
ts.add(new Students("张三", 23, 78.9));
ts.add(new Students("李四", 45, 90.99));
ts.add(new Students("王五", 45, 90));
ts.add(new Students("赵六", 34, 89));
ts.add(new Students("小明", 14, 89.7));
Iterator<Students>>
System.out.println("姓名\t年龄\t分数");
while (it.hasNext()) {
Students>
System.out.println(stu.getName() + "\t" + stu.getAge() + "\t"
+ stu.getscore());
}
}

}
class Students implements Comparable<Object> {
private String name;
private int age;
private double score;

public int compareTo(Object obj) {
if (!(obj instanceof Students))
throw new RuntimeException("不是学生类型");
Students>
if (this.age > s.age)
return 1;
if (this.age == s.age)
return 0;
return -1;
}

Students(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public double getscore() {
return score;
}
}

----------------------ASP.Net+Android+IOS开发、.Net培训、期待与您交流! --------------------  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐