简体   繁体   English

排序ArrayList-IndexOutOfBoundsException -Java

[英]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). 我正在尝试根据我存储在另一个带有整数(结果)的arrayList中的值对具有字符串(PlayersNames)和imageIcons(PlayersIcons)的ArrayList进行排序。 As you can see i get an indexOutOfBoundsException but i cant understand why. 如您所见,我得到了indexOutOfBoundsException但我不明白为什么。 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. 当循环到达arrayList的末尾时,您试图使项目超出列表的末尾。 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. 如果i = 9,且arrayList具有10个项目,则results.get(9)将为您提供列表中的最后一个项目。 results.get(10) will try to get something that doesn't exist. results.get(10)将尝试获取不存在的内容。

You can use Collections.sort(Pass ArrayList Here) , You need not to write your own method. 您可以使用Collections.sort(Pass ArrayList Here) ,而无需编写自己的方法。 Java provides it. Java提供了它。

Eventually i can hold a value of PlayersNames.size()-1 . 最终, i可以保留PlayersNames.size()-1 I can only assume that results has the same size as PlayersNames , or rather PlayersNames.size() == results.size() . 我只能假设results的大小与PlayersNames相同,或更PlayersNames.size() == results.size() ,就是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. 如果这是真的,那么最终您要在results请求results.size()个元素(通过results.get(i+1) ),这比results还要多,因此抛出了IndexOutOfBoundsException 。

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. 简而言之,如果结果包含N个项目,则使用索引N-1访问第N个项目,但是您尝试访问的索引N处的项目不存在。

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. 同样,您的内部循环似乎没有被使用,但是如果您尝试使用j的第一个值访问数组中的某个内容,则出于相同的原因,您可能会遇到相同的问题。

The very last iteration of the for loop, i will equal PlayersNames.size() - 1 . for循环的最后一次迭代, i将等于PlayersNames.size() - 1 On the line where the error occurred, you're calling results.get(i + 1) , which evaluates to results.get(PlayersNames.size()) . 在发生错误的那一行,您正在调用results.get(PlayersNames.size()) results.get(i + 1) ,其results.get(PlayersNames.size())results.get(PlayersNames.size())

Couple of mistakes that I can see : 我可以看到几个错误:

1) 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. EVERYWHERE(不仅在if中),因此当您到达最后一个元素时,您将始终遇到indexOutOfBoundsException。

Either reduce the range of i or remove the +1; 减小i的范围或删除+1;或者

2) you declare a variable 2)你声明一个变量

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) 最简单的方法是仅将外循环迭代到n-1(其中n是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. 排序可能比使用两个ArrayList更有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM