alles ok:
for (int i = items.size() - 1; i > 0; i--) {
first = items.get(i);
second = items.get(i -1);
first.pull(second);
}
nicht alles ok:
//for (int i = 0; i < items.size(); i++) { <----- warum gibt es hier eine indexOutOfBounds Exception und beim herunterzählen nicht?
// first = items.get(i);
// second = items.get(i + 1);
// first.pull(second);
//}
hidden #10625598
Level 23
warum bekommt man wenn man eine ArrayList in eine richtung mit +1 ausgiebt eine IndexOutOfBounds und in die andere richtung nicht?
In der Diskussion
Kommentare (2)
- Beliebt
- Neu
- Alt
Du musst angemeldet sein, um einen Kommentar schreiben zu können
Guadalupe Gagnon
3 Juni 2020, 17:33
This line will throw an error in the commented out loop when i is equal to the last index (the last index + 1 will be out of bounds)
The top code will break when i = 0 so when second = items.get(i -1); is reached the get method will not try to access a -1 index
0
hidden #10625598
3 Juni 2020, 17:42
sounds legit, so easy actually, thanks
0