1

所以我试图删除 SimpleGraph(无向图,JGraphT)的所有边,但由于某种原因,我不断收到 ConcurrentModificationException。

这是我正在尝试做的事情:

首先,我有一个类点如下:

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,其顶点是 Point 的实例并存储在二维数组中

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?

4

1 回答 1

1

ConCurrentModificationException表示您正在尝试修改项目为什么不允许它,即当您尝试在迭代时修改 Collection 时发生

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

第二件事,在你的Point.equal()方法中,你能解释一下为什么你要把点投射到细胞上吗?

于 2013-01-17T15:26:04.840 回答