I'm not sure what is going on here. Any enlightenment would be greatly appreciated.
ArrayList<Integer> al = new ArrayList<>();
for(int i = 0; i < 10; i++)
al.add(i);
for(Integer i : al)
System.out.println(al.get(i));
al.add(2,8); //should add the value 8 to index 2?
System.out.println();
for(Integer i : al)
System.out.println(al.get(i));
Output
0
1
2
3
4
5
6
7
8
9
0
1
7
8
2
3
4
5
6
7
8
Why is it adding in 7 and 8...and where did 9 go?
for (Integer i : al)) are really foreach loops... they give you access to the elements of a collection; you don't need to touch the collection within the loop. In other words, eachiis an integer from inside the collection, not an index like in the first (and only) for loop