Collections.sort(list);
System.out.println("按成绩排序后:" + list);
Collections.reverse(list);
System.out.println("按成绩逆序排列:" + list);
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student student = it.next();
System.out.println(student.name + " " + student.score);
}此代码实现不了5位学生的成绩排序,求解.
System.out.println("按成绩排序后:" + list);
Collections.reverse(list);
System.out.println("按成绩逆序排列:" + list);
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student student = it.next();
System.out.println(student.name + " " + student.score);
}此代码实现不了5位学生的成绩排序,求解.
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
// 我也不晓得你要咋比较,自己写吧
return 0;
}
})
List list = new ArrayList();
Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.score > s2.score)
return 1;
else if (s1.score > s2.score)
return -1;
return 0;
} });
System.out.println("按成绩排序后:" + list);
Collections.reverse(list);
System.out.println("按成绩逆序排列:" + list);
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student student = it.next();
System.out.println(student.name + " " + student.score);
}