The for-each loop will iterate through the values of your list and assign the value to i.
So i get's the values of the elements in your list. Then you try to get the element of the
list at the index of i. And since your list contains a bigger value then the list size it
throws an IndexOutOfBoundsException because you're trying to acces element number 9
when there are only 8 elements.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Apr 27, 2010
Thank You very much wouter.
I actually thought i does not contain the value at that iteration but the "index" for that iteration.
Thnks again.I modified the program and now it runs perfectly
Just used a different variable int pos =0 and for-each loop required just a small modification: