1

With the snippet below I am, attempting to process a spreadsheet, with the twist of needing to exclude ad hoc columns. I know the crude way I am doing it, put the exceptions in an ArrayList and process the list on each and ever increment over the current row columns is perverse, but you know just get it done.

However I am getting the titled error, which I believe should never happen. I am just looping through the ArrayList and comparing, not modifying anything. Where is the error? Is there a better way to handle the exceptions 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) {...
Phil Gan
  • 2,813
  • 2
  • 29
  • 38
cp.
  • 1,241
  • 5
  • 15
  • 26

4 Answers4

4

You can't alter the contents of an Iterable once you create an iterator on it (other than via the iterator), otherwise you'll get a ConcurrentModificationException as soon as you move the iterator - you create an iterator, then do noProcess.add(new Integer("5"));, then later advance the iterator.

Also, you create two iterators - you shouldn't do that either - it's crazy.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • If you're using the iterator's add method then the iterator's cursor get's updated automatically. – Varun Achar Jul 22 '11 at 14:06
  • ahhh yeah - I should have spelled it out that when I said "change" I meant *not* via an iterator. answer edited... – Bohemian Jul 22 '11 at 14:13
  • Sorry all, I really did know that just get so presbyopic cant see the simple syntax issues. However it is more obscure with that clarity. With log4j statement immediately within the loop and within the comparison, I get half of what I expect. I see the first diagnostic twice on each run, for each of the items in the exception list, but NEVER see it for the comparison and therefore include is always true. That is a simple comparison why, he whines. – cp. Jul 22 '11 at 14:52
1

From the JavaDoc,

ConcurrentModificationException : This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

E.g it is not generally permssible for one thread to modify a Collection while another thread is iterating over it.

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
1

From the Java Docs:

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Use the iterator's add method to add an element into the List

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
0

Adding records in list after getting iterator, correct that.. it should work then.

sudmong
  • 2,036
  • 13
  • 12