对一个对象列表进行排序,使用另一个对象的方法。

huangapple go评论74阅读模式
英文:

Sort A List of an Object with other object's method

问题

在下面的代码中,使用这样的类,如何按照课程列表中学生所获得的分数对学生的ArrayList进行排序?我的意思是如何按照另一个类的子类的属性(该属性是另一个类的子类)对特定类的List进行排序。

public class Student{
    public ArrayList<Student> students = new ArrayList<Student>();
    public ArrayList<Course> studentCourses = new ArrayList<Course>();
    //...
}

class Course{
    double score;
    
    public double getScore(){
        return this.score;
    }
    //...
}
英文:

In the bellow code, with such classes, how can I sort an ArrayList of Students by the score the got from Courses List? I mean how can I sort a List of a specific class by an attribute of this class which is a child of another class.

public class Student{
    public ArrayList&lt;Student&gt; students  = new ArrayList&lt;Student&gt;();
    public ArrayList&lt;Course&gt; studentCourses  = new ArrayList&lt;Course&gt;();
    //...
class Course{
    double score;
    
    public double getScore(){
        return this.score;
    }
    //...
}

答案1

得分: 2

你的"Student"类让人感到困惑。根据名称,这个类应该代表一个单独的学生。但是在这个类内部,你有一个List<Student>,这意味着每个"Student"对象将包含指向零个或多个其他"Student"对象的指针。这种关系代表了什么?为什么一个学生会引用多个其他学生?

你的"Course"类也让人感到困惑,因为它只包含一个分数。如果我在定义一个大学课程,我会想象它代表学校/大学某一年的课程,并且它需要包含以下信息才能完整:

  • 课程名称
  • 课程开始的年份
  • 已注册参加此课程的学生集合
  • 课程涉及的考试/考核
  • 每个学生在课程中获得的总体分数或成绩

如果这是一个作业练习,并且你希望保持简单,那么你可能可以忽略单独的考试/考核以及开始年份。但你仍然需要决定如何将总体分数映射到每个学生。

假设你创建了一个代表单个学生的"Student"类(并将你的学生数据库类重命名为"Students"),那么你可以在你的"Course"类内部简单地创建一个Map<Student, Score>(如果你的分数只是简单的数值,你可以使用Map<Student, Integer>)。但需要注意的是,如果你将"Student"对象用作Map的键,那么你需要在你的"Student"类中覆盖"equals"和"hashcode"方法,以使它们正常工作。区分人(可能拥有相同的姓名、出生日期等)的最佳方法是在注册时为每个人分配一个序列号。因此,你的"Student"类可以简单地包含一个带有ID序列号的"long"字段,然后你的"equals"和"hashcode"方法只需检查并使用这一个唯一值。

或者,你可能更喜欢在"Student"类内部添加一个映射,即Map<Course, Score>,以便每个学生跟踪他们的课程分数。你的选择将取决于你的应用程序最常访问的分数类型(获取每个学生的课程分数,还是获取每门课程的学生分数,或者两者兼有)。

但总的来说,你创建的每个类都需要代表一种事物,你需要准确地决定是什么事物。然后,你需要决定每个类需要哪些字段才能完整地代表这个事物。

英文:

Your "Student" class is confusing. Based on the name, it sounds as though this class should represent a single student. But then you have within it a List&lt;Student&gt; which means that every Student object will contain pointers to zero or more other Student objects. What does this relationship represent? Why does a student refer to numerous other students?

Your "Course" class is also confusing, as it contains only a score. If I was defining a university course then I'd imagine it would represent a course at the school/college/university for a particular year, and it would need to contain the following information to be complete:

  • name of the course
  • year in which the course starts
  • the set of students who have registered to take this course
  • the tests/exams/assessments involved in the course
  • the overall score or grade achieved by each student in the course

If this is a homework exercise and you're keeping it simple, then you could probably ignore the individual tests/exams/assessments and also the start year. But you would still need to decide on a way to map the overall score to each student.

Assuming you create a "Student" class which represents a single student, (and rename your student database class to "Students") then you could simply create a Map&lt;Student, Score&gt; inside your "Course" class. (You can use Map&lt;Student, Integer&gt; if your scores are simply numeric values.) But note that if you use the Student objects as Map keys then you will need to override the equals and hashcode methods within your "Student" class so that they work correctly. The best way to distinguish between people (who may have the same names, birthdates, etc) is to assign each one a serial number when they register. So your "Student" class could simply contain a long field with ID serial number, and then your equals and hashcode methods would simply check and use this one unique value.

Alternatively, you might prefer to add a map within the "Student" class, Map&lt;Course, Score&gt; so that each student keeps track of their course scores. What you choose will depend on how your application will most frequently be accessing scores (getting course scores per student, or student scores per course, or both).

But the overall message is that each class you create needs to represent one thing, and you need to decide precisely what. Then you need to decide which fields each class needs in order to fully represent that thing.

huangapple
  • 本文由 发表于 2020年10月18日 20:51:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64413531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定