Strangly enough, this small piece of code throws the above mentioned Exception.
Also, looking at code posted around the web this seems to be correct:
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorTest {
ArrayList<Integer> arr = new ArrayList<Integer>();
Iterator i = arr.iterator();
public void show() {
arr.add(2);
arr.add(5);
arr.add(9);
while(i.hasNext()){
System.out.println(i.next());
}
}
}
Any advice?
Thanks
This call:
should be after you’ve done all the writes into your ArrayList.
So in your code do this just before you start iterating like this: