2013-01-17 58 views
0

所以我试图删除SimpleGraph(无向图,JGraphT)的所有边缘,但由于某些原因我不断收到ConcurrentModificationException。删除接触给定顶点的所有边缘

这里就是我想要做的事:

首先,我作为fowllowed一类Point:

class Point 
{ 
    private int x; 
    private int y; 

    public Point(int x, int y) 
    { 
     this.x = x; 
     this.y = y; 
    } 

    //getters and setters 

    public boolean equals (Object rhs) 
    { 
     if (rhs == null || !(rhs instanceof Point)) 
     return false; 
     else 
     { 
     Point rhsPoint = (Point) rhs; 
     return rhsPoint.x == this.x && rhsPoint.y == this.y; 
     } 
    } 

    public int hashCode() 
    { 
     int hash = 3; 
     hash = 83 * hash + (int) (this.col^(this.col >>> 32)); 
     hash = 83 * hash + (int) (this.row^(this.row >>> 32)); 
     return hash; 
    } 
} 

而且其顶点是点的实例,并存储在一个二维图G array

Point[][] pointContainer = new Point[100][100]; 
SimpleGraph<Point, DefaultEdge.class> g = new SimpleGraph<Point, DefaultEdge.class>(); 

public void initGraph() 
{ 
    for (int row = 0; row < 100; ++row) 
    for (int col = 0; col < 100; ++col) 
    { 
     Point p = new Point(col, row); 
     pointContainer[row][col] = p; 
     g.addVertex(p); 
    } 

    //Then I added edges between any adjacent vertices 
    //so except for those vertices near the edges of the grid, each vertex has 8 edges 

} 

public void removeEdges(int row, int col) 
{ 
    Set edges = g.edgesOf(pointContainer[row][col]); 
    g.removeAllEdges(edges); 
} 

谁能告诉我我在这里做错了什么?为什么我不断收到ConCurrentModificationException?

回答

1

ConCurrentModificationException意味着你正在试图修改项目为什么它是不允许的, 即当您尝试修改集合,当你迭代它

尝试检查你的堆栈跟踪,以帮助您检测错误,很可能碰巧发生在你没有附加的代码中。我认为你调用removeEdges()的地方可能会引起一些问题

第二件事,在你的Point.equal()方法中,你能解释一下为什么你要在点对点单元格中投射?

+0

对不起,这是一个错字。我打算把rhs投给Point。 – 0x56794E