根据分数比较学生JAVA [英] Comparing students according to score JAVA
问题描述
我有一个学生班.该类具有String name
、int studentId
和int studentScore
.我想比较ArrayList
根据学生分数.这意味着如果学生 1"和学生 2"的学生分数相同,那么他们的排名将相同.我有一个设置等级的功能.但我不确定如何比较数组列表中的所有内容.
I have a class of Student. The class has String name
, int studentId
and int studentScore
. I want to compare the students in ArrayList<Student> students
according to the studentscore. This means if "student 1" and "student 2" has the same amount of studentscore then both of their ranks will be the same. I have a function where I set The rank. But I'm unsure how to compare everything in the arraylist.
当我搜索比较 int 值时,我得到了结果:
When I search for comparing int values I get the results:
我认为最好的解决方案是使用比较器类函数.但我不确定如何做到这一点,因为我不确定比较器类的功能.我想到了 for 循环通过该循环获取播放器,然后再次循环并进行比较.但这似乎不是一个好的解决方案.
I think the best solution will be to use comparator class function. But Im unsure how to do this because I'm unsure of the comparator classes capability. I was think of for looping getting a player by that loop then for looping again and comparing. But this doesnt seem like a good solution.
我想要这个结果:
假设学生数组包含 (name,studentscore) 是:
Say that the array of students contains (name,studentscore) is:
(kim, 12), (werder, 20), (ben, 32), (sara, 12)
我希望学生的排名是(如果按 ASC 排序):
I want the rank of the student to be (If ASC order):
kim: rank = 1
werder: 2
ben: 3
sara: 1
推荐答案
您应该使用 java Stream (Java 8).更好的方法来做到这一点.您可以在以下位置找到一个非常好的示例:http://www.leveluplunch.com/java/tutorials/007-sort-arraylist-stream-of-objects-in-java8/
You should use java Stream (Java 8). Better way to do this. You can found a really good exemple at : http://www.leveluplunch.com/java/tutorials/007-sort-arraylist-stream-of-objects-in-java8/
这篇关于根据分数比较学生JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!