1

Hey there guys and gals.

Background: I'm working on a high score program for homework that asks for 5 names and 5 scores. After the names with the corresponding scores are entered the program sorts the two ArrayLists by highest score. Finally it displays the names with their scores in sorted order.

Question: I'm having the devil of a time trying to sort the ArrayLists , do you any advice on sorting ArrayLists ?

Code:

import java.util.*;

public class Assignment6
{
    public static void main(String args[])
    {
        ArrayList<String> names = new ArrayList();
        ArrayList<Integer> scores = new ArrayList();

        initializeArrays(names, scores);
        //sortArrays(names, scores);
        displayArrays(names, scores);
    }

        public static void initializeArrays(ArrayList names, ArrayList scores)
        {
            Scanner in = new Scanner(System.in);
            for(int i=0; i<5; i++)
            {
                System.out.println("Enter the name for score # " + (i+1) + ": ");
                names.add(in.next());
                System.out.println("Enter the score for score # " + (i+1) + ": ");
                scores.add(in.next());
            }
        }

        public static void sortArrays(ArrayList names, ArrayList scores)
        {
            for(int i=0; i<5; i++)
            {
                if(scores[i] < scores[i+1])
                {
                    Collections.swap(scores,a, b);
                    Collections.swap(names,a, b);

                }
            }
        }

        public static void displayArrays(ArrayList names, ArrayList scores)
        {
            System.out.println("Top Scorers: ");
            System.out.println(names);
            System.out.println(scores);
        }


}
2
  • What issues are you having? Commented Sep 27, 2013 at 5:40
  • Collections.swap() is a handy utility for your purposes! But what are a and b? They aren't defined anywhere and your compiler messages should be yelling as much. Commented Sep 27, 2013 at 5:44

3 Answers 3

4

Create one object with fields: name and score with implements Comparable.
Then having ONLY one ArrayList use Collections.sort(list);

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

7 Comments

Why should anyone doing a basic homework assignment use Comparable?
@clwhisk Because it's simpler and better approach.
It's simpler to order the list oneself. Once you understand how to compare elements, it doesn't matter where you put that code, you can just accomplish sorting with it.
@clwhisk It's simpler to use what already exists and not to waste time for it.
Generally using a preexisting sort method is great, but quite the opposite and forbidden for beginner homework.
|
1

You can wrap both score and name into one object and store it in a list.

class Result implements Comparable<Result>{

    private String name;

    private int score;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Result other) {
        return this.score - other.score;
    }

}

Now you can use Collections.sort(List<Result>) to sort them out based on the highest score.

2 Comments

Have you seen Alex's answer, why you have added duplication ?
@SergiiZagriichuk At least it removes one layer of abstraction from that.
0

ok, do you want to print something like that A-{Bob, Alex, ...}, where Bob is a name and A is scope, you can do it using one object as described by Alex, but if its home work I think your teacher want to see some comuter science data structure, in that case is Associative_array will be better. You can implement it by your side or use java implementation. Java provide us Map [T, V] , and implementation, for your case is TreeMap, where T - is scope and V - is a List of the Name because a lot of people can have the same scope. So, result structure will be like that

Map<String, List<String>> sortedScopes = new TreeMap<>();

and using :

List<String> names  = sortedScopes.get(scope);
if(names == null){
  names = new ArrayList<>();
sortedScopes.put(scope, names);
}

names.add(name)

In that solution you will have just a 2 methods initialize and display, Sorting by scope will execute on demand

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.