java - 根据总值对对象数组列表进行排序

标签 java oop sorting arraylist

我计算每个学生的总分。然后我将这些对象添加到数组列表中。我想要做的是基于这个总分(不是 Student 对象的属性),对我的数组列表进行排序并显示带有总分的学生详细信息。我不知道该怎么做。有人可以建议我一个好的优化方法来完成这项工作吗?

public static void TotalModuleMarkAboveAvg(){
        double totalModuleMarkAvg = OverallClassAverage(false); //getting the avg of the total marks
        ArrayList<Student> students = new ArrayList<Student>();
        double studentTotalModuleMark = 0.0;
        student = new Student();
        System.out.println("List of students whose total module marks fall below the class average ");
        for(int i=0; i<MAX_STUDENT; i++){
            Student student = vector.get(i);
            studentTotalModuleMark = student.getCoursework1()*20/100;
            studentTotalModuleMark += student.getCoursework2()*20/100;
            studentTotalModuleMark += student.getFinalExam()*60/100;
            if(studentTotalModuleMark > totalModuleMarkAvg){
                students.add(student);
            }
     // here I want to sort my array list based on the studentTotalModuleMark and display student registration number and total module mark
        }

    }

学生类

public class Student implements java.io.Serializable{
    private int registrationNumber;
    private int coursework1;
    private int coursework2;
    private int finalExam;

    public int getRegistrationNumber() {
        return registrationNumber;
    }
    public void setRegistrationNumber(int registrationNumber) {
        this.registrationNumber = registrationNumber;
    }
    public int getCoursework1() {
        return coursework1;
    }
    public void setCoursework1(int coursework1) {
        this.coursework1 = coursework1;
    }
    public int getCoursework2() {
        return coursework2;
    }
    public void setCoursework2(int coursework2) {
        this.coursework2 = coursework2;
    }
    public int getFinalExam() {
        return finalExam;
    }
    public void setFinalExam(int finalExam) {
        this.finalExam = finalExam;
    }   
}

最佳答案

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学生类(class)。

关于java - 根据总值对对象数组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050546/

相关文章:

java - 从父类(super class)调用子类方法不是最佳实践吗?

xslt - xsl分组排序问题

java - 过滤器中的 ClassCastException?

java - 运行不同版本 Tomcat 的 Tomcat 7 Maven 插件

python - 无法在 python 中调用类函数(TypeError : object() takes no parameters)

java - 在类范围的下一行初始化变量时出错

jquery - 表中的列 - Angular JS

c - 交换函数与快速排序代码一起工作不正确

java - 如何在beanshell中构造Java中的多个对象和JSON数组并查看输出

javax.xml,XPath 不是从具有命名空间的 XML 中提取的