开发者

Sorting ArrayList - IndexOutOfBoundsException -Java

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!

                    tmp=PlayersNames.get(i+1);
                    PlayersNames.set(i+1,PlayersNames.get(i));
                    PlayersNames.set(i,tmp);

                    tmp2=PlayersIcons.get(i+1);
                    PlayersIcons.set(i+1,PlayersIcons.get(i));
                    PlayersIcons.set开发者_开发技巧(i,tmp2);
                }
            }
        }
    }


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 (results.get(i) < results.get(i+1) ) {

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.


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


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.


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()).


Couple of mistakes that I can see :

1)

i<PlayersNames.size()

that is good, but then you use

i+1

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

j

that you never use...


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)

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


Using Map like:

Map <String, ImageIcon>

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜