不哈意思,才看到.以下是我写的.
package heima._A;
import java.util.Comparator;
public class Student implements Comparable<Student>{
private String name;
private int age;
private int score;
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score="
+ score + "]";
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age, int score) {
super();
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(int score) {
this.score = score;
}
@Override
public int compareTo(Student s) {
int num =s.score-this.score;
int num1= this.age-s.age;
return num==0?(num1==0?this.name.compareTo(s.name):num1):num;
}
}
package heima._A;
import java.util.TreeSet;
/*
*
* 1.现有Student类,属性有name, age, score(int类型).
要求 : 按照学生的分数排序, 分数大的童鞋在前面, 如果分数相同, 那么年龄小的在前面, 如果分数年龄都相同, 则按照姓名(英文的即可)的字典顺序排序.要求:不允许在描述类上写排序规则.
下面是给大家学生.
Student("Tom",24, 89)
Student("Robin",32, 99));
Student("Jerry",24, 99));
Student("Lili",23, 87));
Student("Jack",22, 87));
Student("LiLei",25, 95));
Student("Robin",32 ,99));
* */
public class Demo2 {
public static void main(String[] args) {
TreeSet<Student>ts = new TreeSet<>();
ts.add(new Student("Tom",24,89));
ts.add(new Student("Robin",32,89));
ts.add(new Student("Jerry",24,99));
ts.add(new Student("Lili",23,87));
ts.add(new Student("Jack",22,87));
ts.add(new Student("LiLei",25,95));
ts.add(new Student("Robin",32,99));
for (Student student : ts) {
System.out.println(student);
}
}
}
|