0

I've sorted my arraylist in descending order and now I want to print the max value. the arraylist contains a student's name and their grade. I want to print the name of the student with the highest grade. I don't need to print their grade. Just the name. this is my code

public void printStudent(){

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

Right now this prints the entire list of students and their grades however I just want to print the name of the student with the highest grade and I've tried many things but can't seem to get it to work. Thanks!

user2977165
  • 47
  • 1
  • 1
  • 7
  • possible duplicate http://stackoverflow.com/questions/8304767/how-to-get-maximum-value-from-the-list-arraylist –  Nov 13 '13 at 14:50
  • 2
    Basically, your question needs to be reformulated as "How do I get the first item of an array list?", right? – BalusC Nov 13 '13 at 14:51
  • 2
    Do you really need to sort *and* reverse to accomplish this? Is it too hard to just perform one pass with a loop? If you're lazy `Collections.max` is still better. You don't want to optimize prematurely, but I think this is a bit too much for this simple task. – Zong Nov 13 '13 at 14:56

4 Answers4

5

Hint:

  • If you hare sorted a list into ascending order, where is the largest element?

  • If you then reverse the list, where is the largest element now?

  • How do you get the Nth element of a list? (Meta-hint ... read the javadoc!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    This is the only answer that really helps OP to learn instead giving free code to just copy and paste to solve the homework... – Luiggi Mendoza Nov 13 '13 at 14:53
3

Use Collections.max and not sorting.

Lakatos Gyula
  • 3,949
  • 7
  • 35
  • 56
  • This is the right approach in general, but the OP *may* have been instructed to sort and reverse the list ... – Stephen C Nov 13 '13 at 15:03
  • @ZongZhengLi - Why? This is clearly a beginners programming assignment designed to get the student started on the basics of programming. Expecting an "optimal" solution (in any sense) is beside the point for a student at this point in their learning. – Stephen C Nov 13 '13 at 15:22
  • @StephenC I agree, there's no need for perfection. But what do you think when a student can sort and reverse a list, but doesn't know how to access a particular element? I'd argue there are better ways to apply sorting and such in an educational context. – Zong Nov 13 '13 at 15:50
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());
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

How is your Combarable interface setup? Collections.sort is based on this.

So either you can print customerList.get(customerList.size() - 1) to print the last index in your list. Or you can print customerList.get(0)

But as I said: This depends on how you compare Customer against each other.

Joakim Palmkvist
  • 540
  • 2
  • 11