我可以使用此代码中的所有简单比较器进行排序,但不能使用ComplexComparator. 我无法弄清楚如何编码才能使其正常工作。任何建议/解释将不胜感激。这是我的主要程序:package pkgTest;import java.util.Arrays;public class Main { public static void main(String[] args) { Student[] students = new Student[6]; students[0] = new Student("Pete", 1989, 3.6); students[1] = new Student("Tomas", 1989, 3.9); students[2] = new Student("Helen", 1990, 3.6); students[3] = new Student("Steve", 1991, 3.7); students[4] = new Student("Natalie", 1993, 3.7); students[5] = new Student("John", 1992, 4.0); NameComparator byName = new NameComparator(); BirthDateComparator byBirthDate = new BirthDateComparator(); AverageComparator byAverage = new AverageComparator(); ComplexComparator complexSorting = new ComplexComparator(byName, byAverage); System.out.println("==============="); System.out.println("Before sorting:"); System.out.println("==============="); for (Student student : students) { System.out.println(student.getName() + " // " + student.getBirthDate() + " // " + student.getAverage()); } Arrays.sort(students, complexSorting); System.out.println("=============="); System.out.println("After sorting:"); System.out.println("=============="); for (Student student : students) { System.out.println(student.getName() + " // " + student.getBirthDate() + " // " + student.getAverage()); } }}
添加回答
举报
0/150
提交
取消