I understand that the array list produces concurrent modification exception when we try to perform (add/remove) on the list, while iterating over the list.
For example, the following method should throw Concurrent Modification Exception as I try to remove a item, while iterating.
public static void testMe() {
inputList = new ArrayList<String>();
inputList.add("1");
inputList.add("2");
inputList.add("3");
// Try comment the above insertion and uncomment this and run
// for (int i = 0; i < 5; i++) {
// inputList.add(""+i);
// }
System.out.println("List Size:" + inputList.size());
Iterator<String> iterator = inputList.iterator();
while (iterator.hasNext()) {
String value = (String) iterator.next();
if (value.equals("2")) {
inputList.remove(value);
System.out.println("remvoing 2");
}
}
System.out.println("List Size:" + inputList.size());
}
Strangely I don't get one. But if I insert items using a for loop, the exception is thrown. I wonder why this is not happening earlier?