【问题标题】:Sort object array list based on total value根据总值对对象数组列表进行排序
【发布时间】:2013-12-01 18:23:12
【问题描述】:

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

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;
    }   
}

【问题讨论】:

  • 如果没有总数作为字段,排序将是一个真正令人头疼的问题。您需要一个相应的数组来保存总数。在手动对 Arraylist 进行排序时,对相应的数组索引进行排序。为什么不将总数作为一个字段?
  • 另外,我感觉您希望将 ArrayList 放在方法之外。更有意义。
  • 为什么你的方法中有两个学生变量。不好的做法。
  • 是的,因为总可以导出运行时间,以为我不需要它作为字段。我可以改变每个模块运行时的权重。但似乎除了将总计添加为字段之外,我别无选择。
  • 你不需要它,但你真的想要它。如果你有它作为一个字段,你可以使用神奇的Collections.sort() 技巧。只需要让您的方法实现可比较。我会提出一个答案,但要告诉你

标签: java oop sorting arraylist


【解决方案1】:
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,你的Student 类中就不需要compareTo()implements Comparable

【讨论】:

  • 意义重大.. 非常感谢
  • 我不确定您希望如何计算 totalScore,因此请自行计算。
  • 是的,这已经足够了!!
  • 其他问题.. 我可以使用多个“compareTo”对我的 ArrayList 进行多次排序吗?我有点困惑。
  • 您可以对每个组件使用比较器排序。但是请记住,一旦对它进行排序,然后使用不同的比较器对其进行排序,则无法再获得先前的排序。因此,在一个排序之后,您可能想要执行所有操作,然后为不同的组件完成另一个排序。请参阅我的编辑。它对我的课堂作业进行排序1。你不能对所有其他组件做同样的事情。只需将 classwork 切换为其他内容即可。
猜你喜欢
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 2015-01-03
相关资源
最近更新 更多