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
IndexOutOfBoundsExceptionon my computer.iin the lambda? What doesints.get(..)do?