ConcurrentModificationException

With the snippet below, I'm trying to process a spreadsheet, with a twist on the need to exclude special columns. I know how I do this, adding exceptions to the ArrayList and processing the list on each and every increment over the current columns of the row is perverse, but you know how to do it.

However, I get a title error, which, it seems to me, will never happen. I just iterate over ArrayList and compare without changing anything. Where is the mistake? Is there a better way to handle the exception list?

ArrayList noProcess = new ArrayList(); Iterator itr00 = noProcess.iterator(); Iterator itr01 = noProcess.iterator(); noProcess.add(new Integer("5")); noProcess.add(new Integer("18")); .... boolean include=true; for(int i=0;i<archive.length;i++){ for (int j = 0; j < archive[i].length; j++) { while (itr00.hasNext()) { if (j == ( (Integer) itr00.next()).intValue()) include = false; } if (include) {... 
+1
source share
4 answers

You cannot change the contents of Iterable after creating an iterator on it (except through an iterator), otherwise you will get a ConcurrentModificationException exception, as soon as you move the iterator - you will create an iterator, then do noProcess.add(new Integer("5")); and then advance the iterator.

In addition, you create two iterators - you shouldn't do that either - that's crazy.

+4
source

From JavaDoc ,

ConcurrentModificationException : This exception may be thrown by methods that have detected a simultaneous modification if such a modification is unacceptable.

For example, one thread usually does not need to modify the collection, while another thread iterates through it.

+1
source

From Java docs:

The iterators returned by the iterator and listIterator methods from this class do not work quickly: if the list is structurally modified at any time after creating the iterator, in any way, except for the iterator itself deleting or adding methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of parallel modifications, the iterator fails quickly and cleanly, and not at the risk of arbitrary, non-deterministic behavior at an undetermined time in the future.

Use the add iterator method to add an item to the list.

+1
source

Adding entries to the list after receiving the iterator, fix it .. it should work then.

0
source

All Articles