John Doe John Doe - 1 month ago 7
Java Question

ConcurrentModificationException while iterating through List, altough not modifying it

I have following code (UPDATE: I added full code)

public class Triangle {
private Vertex2D a;
private Vertex2D b;
private Vertex2D c;

private boolean divided = false;

public static ArrayList<Triangle> triangles = new ArrayList<>();
public static ArrayList<Triangle> newTriangles = new ArrayList<>();

public Triangle(Vertex2D a, Vertex2D b, Vertex2D c) {
this.a = a;
this.b = b;
this.c = c;
triangles.add(this);
}

public Triangle(Vertex2D a, Vertex2D b, Vertex2D c, int depth) {
this(a,b,c);
if (depth > 0) {
divide(depth);
}
}

public Vertex2D getVertexA() {
return a;
}

public Vertex2D getVertexB() {
return b;
}

public Vertex2D getVertexC() {
return c;
}

public boolean isDivided() {
return divided;
}

public Triangle getSubTriangle(int i) {
if ((!isDivided()) || (i > (triangles.size() - 1))) {
return null;
}
return triangles.get(i);
}

public boolean divide() {
if (isDivided()) {
return false;
}
Vertex2D ac = new Vertex2D((getVertexA().getX() + getVertexC().getX()) / 2, (getVertexA().getY() + getVertexC().getY()) / 2);
Vertex2D bc = new Vertex2D((getVertexB().getX() + getVertexC().getX()) / 2, (getVertexB().getY() + getVertexC().getY()) / 2);
Vertex2D ab = new Vertex2D((getVertexA().getX() + getVertexB().getX()) / 2, (getVertexA().getY() + getVertexB().getY()) / 2);

Triangle t1 = new Triangle(a, ab, ac);
Triangle t2 = new Triangle(ab, b, bc);
Triangle t3 = new Triangle(ac, bc, c);

newTriangles.add(t1);
newTriangles.add(t2);
newTriangles.add(t3);
divided = true;
return true;
}
public boolean divide(int depth) {
if (depth == 0) return false;
while (depth > 0) {
newTriangles.clear();
for (Iterator<Triangle> iterator = triangles.iterator(); iterator.hasNext();) {
Triangle t = iterator.next();
t.divide();
}
triangles.addAll(newTriangles);
divide(depth-1);
return true;
}
return true;
}


}

Vertex2D:

public class Vertex2D {

private double x;
private double y;

public Vertex2D(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double distance(Vertex2D v) {
if (v == null) return -1.0;
return Math.sqrt(Math.pow(this.x - v.getX(),2.0) + Math.pow(this.y - v.getY(),2.0));
}

public String toString() {
return "[" + x + ", " + y + "]";
}


}

Calling the method

public static void main(String[] args) {
Vertex2D a = new Vertex2D(-100,0);
Vertex2D b = new Vertex2D(0,100);
Vertex2D c = new Vertex2D(100,-100);
try {
Triangle triangle = new Triangle(a, b, c, 3);
} catch (ConcurrentModificationException e) {
e.printStackTrace();
}
}


Everytime I run it I get the ConcurrentModificationException. Now, I know, that this issue has been discussed here many times, but the reason for throwing this exception, was always in adding/removing element from list while iterating it and this is not the case. In my recursive method I create empty temp list, and through iteration I'm only adding new elements to the temp list, leaving iterated list untouched, until the end of the iteration. But I still get the exception. Why is this?

Answer

While iterating through triangles:

for (Iterator<Triangle> iterator = triangles.iterator(); iterator.hasNext();) {
      Triangle t = iterator.next();
      t.divide(); //in divide you add new triangle to triangles
    }

You modify tiangles in construcor in method divide():

divide() {
    //....
    Triangle t1 = new Triangle(a, ab, ac);
    //...
}


public Triangle(Vertex2D a, Vertex2D b, Vertex2D c) {
  this.a = a;
  this.b = b;
  this.c = c;
  triangles.add(this);  //this is the place where you modify triangles
}