62,614
社区成员
发帖
与我相关
我的任务
分享
List<Student> students = new ArrayList<Student>();
Collections.sort(students);
for(Student stu : students.subList(0, 5)) {
System.out.println(stu);
}
class Student implements Comparable<Student> {
int sno;
float chinese;
float math;
float english;
float sum;
public Student() {
}
public Student(int sno, float _chinese, float _math, float _english) {
this.sno = sno;
this.chinese = _chinese;
this.math = _math;
this.english = _english;
this.sum = _chinese + _math + _english;
}
@Override
public int compareTo(Student o) {
if(this.sum > o.sum) {
return 1;
} else if(this.sum == o.sum) {
if(this.chinese > o.chinese) {
return 1;
} else if(this.chinese == o.chinese) {
if(this.sno > o.sno) {
return 1;
} else {
return -1;
}
} else {
return -1;
}
} else {
return -1;
}
}
@Override
public String toString() {
return "Student [sno=" + sno + ", chinese=" + chinese + ", math="
+ math + ", english=" + english + ", sum=" + sum + "]";
}
}