0

我已经按降序对数组列表进行了排序,现在我想打印最大值。arraylist 包含学生的姓名和他们的成绩。我想打印成绩最高的学生的姓名。我不需要打印他们的成绩。只是名字。这是我的代码

public void printStudent(){

    Collections.sort(studentList);
    Collections.reverse(studentList);
    System.out.println("Best student is: ");
    for (Student s: studentList){
        System.out.println(s.toString());
    }
}

现在这会打印整个学生列表和他们的成绩,但是我只想打印成绩最高的学生的名字,我已经尝试了很多东西,但似乎无法让它发挥作用。谢谢!

4

4 回答 4

5

暗示:

  • 如果您将列表按升序排序,最大的元素在哪里?

  • 如果然后将列表反转,那么现在最大的元素在哪里?

  • 你如何获得列表的第 N 个元素?(元提示...阅读 javadoc!)

于 2013-11-13T14:52:16.330 回答
3

使用Collections.max而不是排序。

于 2013-11-13T14:52:40.047 回答
0

You already sorted the Customer in reverse order then the highest grade should be in the first position of your collection.

Do like below.

Customer c = customerList.get(0); // getting the first elementt
System.out.println(c.toString());
于 2013-11-13T14:50:23.260 回答
0

您的 Combarable 接口设置如何?Collections.sort 基于此。

因此,您可以打印 customerList.get(customerList.size() - 1) 来打印列表中的最后一个索引。或者您可以打印 customerList.get(0)

但正如我所说:这取决于您如何将客户相互比较。

于 2013-11-13T14:52:39.533 回答