在迭代List时,ConcurrentModificationException,而不是修改它

时间:2016-10-20 07:29:34

标签: java arraylist iteration concurrentmodification

我有以下代码(更新:我添加了完整代码)

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 + "]";
}

}

调用方法

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();
    }
}

每次我运行它都会得到ConcurrentModificationException。现在,我知道,这个问题已经在这里讨论了很多次,但抛出这个异常的原因总是在迭代它时从列表中添加/删除元素,而事实并非如此。在我的递归方法中,我创建了空的临时列表,并且通过迭代,我只是将新元素添加到临时列表中,使迭代列表保持不变,直到迭代结束。但我仍然得到例外。为什么是这样?

2 个答案:

答案 0 :(得分:2)

迭代三角形时:

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

你可以在方法divide()中修改construcor中的tiangles:

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
}

答案 1 :(得分:-1)

前几天我有一些类似的经历,我发现你应该使用迭代器而不是foreach

for (Iterator<Triangle> iterator = triangles.iterator(); iterator.hasNext();) {
    Triangle t = iterator.nect();
    t.divide();
}