2013-01-17 47 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

相關問題