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!

3
  • possible duplicate stackoverflow.com/questions/8304767/… Commented Nov 13, 2013 at 14:50
  • 2
    Basically, your question needs to be reformulated as "How do I get the first item of an array list?", right? Commented Nov 13, 2013 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. Commented Nov 13, 2013 at 14:56

4 Answers 4

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!)

Sign up to request clarification or add additional context in comments.

1 Comment

This is the only answer that really helps OP to learn instead giving free code to just copy and paste to solve the homework...
3

Use Collections.max and not sorting.

3 Comments

This is the right approach in general, but the OP may have been instructed to sort and reverse the list ...
@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.
@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.
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());

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.