Не удалось найти точный путь хранения зерна в мастер

public class Student implements Comparable<Student>, Serializable {
    private int registrationNumber;
    private int coursework1;
    private int coursework2;
    private int finalExam;
    private int totalScore;

    public Student(int registrationNumber, int coursework1, 
                             int coursework2, int finalExam) {
        this.registrationNumber = registrationNumber;
        this.coursework1 = coursework1;
        this.coursework2 = coursework2;
        this.finalExam = finalExam;
        totalScore = coursework1 + coursework2 + finalExam;
    }

    ...
    // all you getters and setters
    ...



    public int compareTo(Student s){
        if (this.totalScore > s.totalScore)
            return 1;
        else if (this.totalScore == s.totalScore)
            return 0;
        else 
            return -1;
    }
}

Теперь все настроено для использования метода Collections.sort()

ArrayList<Student> students = new ArrayList<Student>();

Student student1 = new Student(1, 90, 70, 100);
Student student1 = new Student(2, 85, 43, 90);
Student student1 = new Student(3, 67, 70, 80);

students.add(student1);
students.add(student2);
students.add(student3);

Collections.sort(students);
// Magic!

Редактирование: использование анонимного компаратора

public class ComparatorPractice{
    public static void main(String[] args){
        ArrayList<Student> students = new ArrayList<Student>();

        Student student1 = new Student(1, 90, 70, 100);
        Student student1 = new Student(2, 85, 43, 90);
        Student student1 = new Student(3, 67, 70, 80);

        students.add(student1);
        students.add(student2);
        students.add(student3);

       Collections.sort(students, new Comparator(){
           @Override
           public int compare(Object o1, Object o2){
               if (o1 instanceof Student && o2 instanceof Student){
                   if (o1.coursework1 > o2.coursework1)
                       return 1;
                   else if (o1.coursework1 == o2.coursework1)
                       return 0;
                   else
                        return -1;
               }
           }
       });

       System.out.println("Highest coursework1 mark is " 
                           + students.get(students.size()- 1).coursework1);

       Collections.sort(students, new Comparator(){
           @Override
           public int compare(Object o1, Object o2){
               if (o1 instanceof Student && o2 instanceof Student){
                   if (o1.coursework2 > o2.coursework2)
                       return 1;
                   else if (o1.coursework2 == o2.coursework2)
                       return 0;
                   else
                        return -1;
               }
           }
       });

       System.out.println("Highest coursework2 mark is " 
                           + students.get(students.size()- 1).coursework2);
    }
}

Просто сделайте то же самое для каждого компонента, который хотите сортировать. Если вы сделаете это так, не используя Comparable, вам не нужны compareTo() или implements Comparable в вашем классе Student.

0
задан Dheeraj Chelaramani 13 July 2018 в 06:50
поделиться