移除接触给定顶点的所有边

时间:2013-01-17 15:15:56

标签: java graph java.util.concurrent jgrapht

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

这是我想要做的事情:

首先,我有一个类如Fowllowed:

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的实例,并存储在2D数组

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 个答案:

答案 0 :(得分:1)

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

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

第二件事,在你的Point.equal()方法中,你能解释一下为什么你要指向细胞吗?