Why when I use following code I get IndexOutOfBoundsException
Code:
List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
System.out.print(ints.get(i-1) + " ");
});
My error stack:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 5
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at Agent.lambda$main$1(Agent.java:33)
at Agent$$Lambda$8/980546781.accept(Unknown Source)
at java.util.ArrayList.forEach(ArrayList.java:1234)
at Agent.main(Agent.java:32)
the list: Java Result: 1
but When I change my list to one digit numbers everything is fine
Code:
List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
System.out.print(ints.get(i-1) + " ");
});
output:
2 8 7 4 3
forEach
javadoc statesSo the
i
inis each element in the
List
. If any of those elements minus 1 is bigger or equal to 5 (the size of theList
), you try to get an element that is out of bounds.You are overdoing it. What you want is
i
in the forEach is not a loop counter, it's the items themselves. Soi
will take on the values 2,8,7,4,3 and for the second iterationints.get (8-1)
will be out of bounds.I think the reason is pretty clear.
Translates approximately to:
Which will cause
IndexOutOfBoundsException
s becausei
refers to the elements of each list, and each of those elements - 1 will give an index that is clearly out of bounds. For your first example,i
will be21
, which gives an index of21 - 1 == 20
, which is out of bounds for the list you created.Example:
will end up so that
So when you run this:
The computer takes the first element and tries to execute the body of the lambda:
And for your second example:
becomes
So when you run this:
The computer goes through the elements and tries to execute the body of the lambda:
So evidently the code in your second example is not what you actually have. I suspect that the code you actually have is:
Which is entirely different than what you posted.
Even simpler: