ArrayList索引超出范围,但实际上不是吗?

时间:2018-11-25 12:24:43

标签: 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)

1 个答案:

答案 0 :(得分:2)

无论列表的实际大小如何,此代码段都包含一个缺陷:

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    confirm("Press a button!");
}
</script>

Option Explicit Sub Delete() Dim wb As Workbook Dim ws As Worksheet Dim Lastrow As Long, i As Long, RowNum As Long Dim str As String Dim r As Range, Cell As Range Set wb = ThisWorkbook Set ws = wb.Sheets("Blad1") str = "XxX" Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Set r = ws.Range(ws.Cells(1, 1), ws.Cells(Lastrow, 1)) For Each Cell In r If Cell.Value Like "*" & str & "*" Then i = Cell.Row Exit For End If Next Cell For RowNum = i + 1 To Lastrow ws.Rows(i + 1).Delete Next RowNum End Sub 允许if (i + 1 > superstarKandidaten.size()) i = 0; else i++; System.out.println(i); person2 = superstarKandidaten.get(i); 到达if,而有效索引在i ... super....size()的范围内。

例如0super...size()-1i=3不,super...size()=4。然后3+1>4?死了。