0

So, I have tried to create a loop in Java that I want to copy my list of lists of cells, however I get the error java.util.ConcurrentModificationException the second time the inner loop is run. Any ideas why?

Here is the code:

private ArrayList<ArrayList<Cell>> copyBoard(ArrayList<ArrayList<Cell>> board) {
    ArrayList<ArrayList<Cell>> copiedBoard = null;
    for (ArrayList<Cell> array : board) {
        int i = 0;
        for (Cell cell : board.get(i)) {
            Cell copiedCell = new Cell();
            copiedCell = cell;
            array.add(copiedCell);
            i++;
        }
        copiedBoard.add(array);
    }
    return copiedBoard;
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Simon Olsen
  • 605
  • 1
  • 6
  • 14
  • 4
    Please paste your code into the question. Do not put it on an external site. – Sergey Kalinichenko Mar 02 '15 at 13:15
  • (1) `Cell copiedCell = new Cell()` – The newly created cell is not used, the next statement overwrites the cell. (2) `copiedBoard.add(array)` this will throw a `NullPointerException`, because it is not initialized. Initialize the `ArrayList` first using `ArrayList> copiedBoard = new ArrayList>()`. – MC Emperor Mar 02 '15 at 13:27

5 Answers5

0

One problem you have with your code is that copiedBoard is never initialized.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

You're not allowed to modify a generic collection while iterating forward throughout it. Iterate backwards: this way, you save time and space w/o having to copy the collection to a different location.

private ArrayList<ArrayList<Cell>> copyBoard(ArrayList<ArrayList<Cell>> board) {
  ArrayList<ArrayList<Cell>> copiedBoard = new ArrayList<ArrayList<Cell>>(); //initialize it

    for(int i = board.length - 1; i>=0; i--){
        ArrayList<Cell> collection = new ArrayList<Cell>();
        for(int j = board.get(i).length - 1; j>=0; j--){
          Cell cell = board.get(i).get(j);
          collection.add(copiedCell);
        }
    }
    copiedBoard.add(collection);
  }

  return copiedBoard;
}
Gabe
  • 961
  • 1
  • 10
  • 23
  • You are allowed to modify an ArrayList while iterating over it, just not within an enhanced for loop. – aliteralmind Mar 02 '15 at 13:27
  • Read again. Try iterating forward with a simple for. – Gabe Mar 02 '15 at 13:28
  • Well, iterating with a java.lang.Iterator, you're right. I was responding to the *concept* of iterating, not the Iterator object. – aliteralmind Mar 02 '15 at 13:29
  • I was referring to the fact that forward iteration throughout a generic collection having objects added or removed is not permitted. Edited to avoid confusion. – Gabe Mar 02 '15 at 13:33
0

The enhanced for loop you are using, is only a language construct that the compiler internally translates to an Iterator. While you are iterating a collection, you are not allowed to modify it directly, but only via the Iterator's methods.

In line 6 your are effectively getting an iterator on the list that you already retrieved in line 4. So as of line 6 you are iterating the list, that you are then modifying in line 9 via .add().

You can fix it by creating the iterator yourself, and then using the Iterator's modification methods to insert elements.

Michael Heß
  • 161
  • 6
0

Because it is illegal to add to/delete from something being iterated in an enhanced for loop (or with an java.util.Iterator)

Here is an answer I wrote aboout the differences between the traditional and enhanced for loops: How does the Java 'for each' loop work?

Chane your loop(s) to the traditional for loop and it will work fine...as long as you appropriately update the index counter.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

This code does not really copy the cell:

Cell copiedCell = new Cell();
copiedCell = cell;

You are just assigning the same object... You should read this: How do I copy an object in Java?

Community
  • 1
  • 1
David Ruiz
  • 96
  • 3