why following code throwing ConcurrentModificationException? Josh Bloch can avoid ConcurrentModificationException.
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(100);
list.add(200);
list.add(300);
list.add(400);
for(Integer field : list) {
list.remove(field);
list.add(200);
}
You can’t use remove on the list while using the “for each” loop. Instead, you can use this to call remove on the iterator:
If you actually want to replace every value with “200”, or replace with some other value, it might make more sense to build up a new list: