java - 使用其他对象的方法对对象列表进行排序
问题描述
在下面的代码中,对于这样的课程,我如何按从课程列表中获得的分数对学生的 ArrayList 进行排序?我的意思是如何通过该类的属性对特定类的列表进行排序,该属性是另一个类的子类。
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;
}
//...
}
解决方案
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<Student>
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<Student, Score>
inside your "Course" class. (You can use Map<Student, Integer>
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<Course, Score>
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.
推荐阅读
- php - AWS 服务器上 CI 的 Cron 作业执行问题
- sql - 从多个表进行 SQL 查询并获取数据
- python-3.x - Python Multiprocess在函数中找不到字典和列表
- react-native - 视频在 react native webrtc 中不起作用
- spring-boot - 限制 Spring Boot Endpoint 访问特定的 Referrer
- email - 当电子邮件在垃圾邮件中时,电子邮件标题是否不同?
- android - 如果 App 在 Android 中收到通知,Worker manager 会唤醒吗?
- firebase - Firebase 项目未显示在 Google Analytics 中
- python - 自动获取 matplotlib gridspec 的维度或索引
- python - 使用 os.system() 运行命令后 Python 脚本停止运行