简体   繁体   English

遍历List时发生ConcurrentModificationException,尽管未对其进行任何修改

[英]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: 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. 每次我运行它时,都会收到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? 为什么是这样?

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(): 您可以在方法()中的construcor中修改tiangle:

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
}

I had a somewhat similar experience the other day, I found out that you should use the iterator instead of foreach 前几天我也有类似的经历,我发现您应该使用迭代器而不是foreach

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历列表但未修改列表时发生ConcurrentModificationException - ConcurrentModificationException while iterating through a list but not modifying it 通过HashMap迭代时出现ConcurrentModificationException - ConcurrentModificationException while iterating through HashMap 迭代Arraylist时的ConcurrentModificationException(不删除) - ConcurrentModificationException while iterating through Arraylist (not removing) 在遍历元素时删除元素。 removeIf 导致 ConcurrentModificationException - Removing element while iterating through it. removeIf result in ConcurrentModificationException Java ConcurrentModificationException:是否可以在迭代时向哈希表添加元素? - Java ConcurrentModificationException: Is it possible to add elements to a hashtable while iterating through it? 迭代列表时遇到java.util.ConcurrentModificationException - Running into java.util.ConcurrentModificationException while iterating list 为什么我在同时迭代和修改HashMap时没有得到“ConcurrentModificationException”? - How come I am not getting “ConcurrentModificationException” while iterating and modifying on HashMap at the same time? 不修改列表但仍然得到ConcurrentModificationException - Not modifying list but still get a ConcurrentModificationException 迭代ArrayList时出现ConcurrentModificationException <Integer> - ConcurrentModificationException when iterating through an ArrayList <Integer> 遍历集合以添加项目但抛出 ConcurrentModificationException - Iterating through collections to add items but throwing ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM