I have this little piece of code and it gives me the concurrent modification exception. I cannot understand why I keep getting it, even though I do not see any concurrent mo
If the above solutions doesn't work properly. You can use old for-loop for iterating a List at the same time adding new items. See the example below:
import java.util.*;
public class SomeClass {
public static void main(String[] args) {
ArrayList aList = new ArrayList(); // we will iterate this
// this will cause ConcurrentModificationException.
// Since we are iterating the list, at the same time modifying it.
/*for(AClass a: aList){
aList.add(someMethod(a));
}*/
// old fashion for-loop will help
int limit = aList.size();
for(int i=0; ctr