java - ArrayList索引越界但实际上没有?

标签 java arraylist

所以我现在的 java 代码有一个非常令人困惑的错误。我将插入代码的重要部分,并解释它应该做什么:

 ArrayList<String> superstarKandidaten = new ArrayList<>(freundesliste.keySet()); //there are exactly 5 keys in freundesliste, and after checking the content of superstarKandidaten, it gives me 5 different values (everything how I wanted it to be)

现在,下面的代码看起来有点奇怪,它基本上应该读取 superstarKandidaten 的两个不同的值。

int i = 0;    
while (superstarKandidaten.size() > 1) {
                if (i + 1 > superstarKandidaten.size()) i = 0; else i++;
                System.out.println(i);
                person1 = superstarKandidaten.get(i);
                System.out.println(person1);
                if (i + 1 > superstarKandidaten.size()) i = 0; else i++;
                System.out.println(i);
                person2 = superstarKandidaten.get(i); // <-- this is where the error happens according to the error message. (this is Ausrechnen.java:24
                System.out.println(person2);
                if (person1.equals(person2)) {
                    if (i + 1 > superstarKandidaten.size()) {
                        i = 0;
                    }else i++;
                    person2 = superstarKandidaten.get(i);
                }

我不会编写其余的代码,因为它是不必要的,这是输出:

    {Knuth=null Turing Dijkstra, Hoare=null Turing Dijkstra Codd, Turing=Hoare Dijkstra, Codd=null Turing Dijkstra Knuth, Dijkstra=null} // this is freundesliste
[Knuth, Hoare, Turing, Codd, Dijkstra] //this is superstarKandidaten
1 //this is i
Hoare //this is person1
2 //this is i
Turing //this is person2
3
Dijkstra
4
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.get(ArrayList.java:458)
    at Ausrechnen.superstar(Ausrechnen.java:24)
    at Main.main(Main.java:22)

最佳答案

无论您的列表的实际大小是多少,此代码段都包含一个缺陷:

if (i + 1 > superstarKandidaten.size()) i = 0; else i++;
System.out.println(i);
person2 = superstarKandidaten.get(i);

if 允许 i 到达 super....size(),而有效索引在 范围内0... super ...size()-1

例如i=3super...size()=43+1>4? 否,i=4。然后 super...get(4) 就死掉了。

关于java - ArrayList索引越界但实际上没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467405/

相关文章:

Java + ImageMagick : how to figure out height of text based on width?

java - 从 XML 获取自定义 TextView 的自定义属性

java - 在 Android 上使用 CoreNLP “无法返回空树或叶树的头部”

java - 如何将数组变为ArrayList? (泛型)

java - 如何在 J2EE/JSP 中的 MVC2 架构中创建可编辑的数据网格

Java 检查枚举值是否是返回的一组其他枚举值的一部分

java - 如何将元素从 `2d array` 复制到 `Arraylist` ?

java - java中通用对象的ArrayList。作为原始类型 'Class(T)' 的成员对 'Class' 进行未经检查的调用

java - 带有 intent.addExtra(parcelable Object) 的 NotSerializable 异常

java - CSV Java文件读取和保存(在不同的ArrayList中)