Sign up ×

I'm trying to sort an ArrayList with strings(PlayersNames) and imageIcons(PlayersIcons) based on the values i store in an other arrayList with integers(results). As you can see i get an indexOutOfBoundsException but i cant understand why. Maybe the earling of the morning makes me not to see plain things.

ArrayList<String> PlayersNames=new ArrayList<String>;
ArrayList<ImageIcon> PlayersIcons=new ArrayList<ImageIcons>;

    public void sortPlayers(ArrayList<Integer> results){
        String tmp;
        ImageIcon tmp2;
        for (int i=0; i<PlayersNames.size(); i++) {
            for (int j=PlayersNames.size(); j>i; j--) {

                if (results.get(i) < results.get(i+1) ) {       //IndexOutOfBoundsException!


share|improve this question
Well obviously you're going off the end of the results list, but you haven't posted the code that calls sortPlayers() so we can't tell how big that list is supposed to be. Is it the same size as PlayersNames? If so, then the last iteration of your loop, when i == PlayersNames.size() - 1, will cause you to go out of bounds because trying to call results.get (i + 1) will be one too many for the results list. –  Stewart Murrie Jan 13 '11 at 5:08

7 Answers 7

When the loop gets to the end of the arrayList, you are trying to get an item past the end of the list. On this line:


If i=9, with an arrayList with 10 items, results.get(9) will give you the last item in the list. results.get(10) will try to get something that doesn't exist.

share|improve this answer

You can use Collections.sort(Pass ArrayList Here), You need not to write your own method. Java provides it.

share|improve this answer
problem is that i dont want to sort an ArrayList only. I want to sort it based on the results arraylist. Just think that results are what players turn with the rolling of the dice and playernames their names. What i wanna do is not to sort their names only but their names list according their rolling of dice. thanx –  FILIaS Jan 13 '11 at 12:55

Eventually i can hold a value of PlayersNames.size()-1. I can only assume that results has the same size as PlayersNames, or rather PlayersNames.size() == results.size().

If this is true, then eventually you're asking for the results.size()-th element (by doing results.get(i+1)) in results, which is one more than results holds, thus an IndexOutOfBoundsException is being thrown.

Said more concisely, if results holds N items, then the N-th item is accessed using the index N-1, but you're trying to access the item at index N, which doesn't exist.

Try changing your outer loop to:

for (int i=0; i<PlayersNames.size()-1; i++) {

to prevent the overrun.

Also, your inner loop appears to be unused, but if you try to access something in one of your arrays using the first value of j you're likely to run into the same issue for the same reason.

share|improve this answer
how can i use j on this occasion??i tried only to sort it based on bubblesort :/ –  FILIaS Jan 13 '11 at 12:59

The very last iteration of the for loop, i will equal PlayersNames.size() - 1. On the line where the error occurred, you're calling results.get(i + 1), which evaluates to results.get(PlayersNames.size()).

share|improve this answer

Couple of mistakes that I can see :



that is good, but then you use


EVERYWHERE (not only in the if), so when you reach the last element you will always hit an indexOutOfBoundsException.

Either reduce the range of i or remove the +1;

2) you declare a variable


that you never use...

share|improve this answer

Many people have given the correct reasons.

There are many ways to correct this program. Easiest one is to iterate outer loop only till n-1 (where n is the size of the ArrayList)

share|improve this answer

Using Map like:

Map <String, ImageIcon>

may be more useful for sorting, rather than using two ArrayLists.

share|improve this answer
but how can i use map when i want both the 2 arrayLists on other cases? :/ –  FILIaS Jan 13 '11 at 12:58

Your Answer


