简体   繁体   English

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

[英]Remove all edges touching a given vertex

So I'm trying to remove all edges of a SimpleGraph (undirected graph, JGraphT) but for some reason I keep getting ConcurrentModificationException. 因此,我试图删除SimpleGraph(无向图,JGraphT)的所有边缘,但是由于某些原因,我一直在获取ConcurrentModificationException。

Here's what I'm trying to do: 这是我想做的事情:

First, I have a class Point as 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;
   }
}

And a graph g whose vertices are instances of Point and are stored in a 2D array 图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);  
}

Can anyone tell me what I did wrong here? 谁能告诉我我在这里做错了什么? why do I keep getting ConCurrentModificationException? 为什么我不断收到ConCurrentModificationException?

ConCurrentModificationException means you are trying modify item why it is not permitted, ie occurs when you try to modify Collection when you iterating it ConCurrentModificationException表示您正在尝试修改项目,为什么不允许这样做,即在迭代时尝试修改Collection时发生

try to check your stack trace to help you detect error, very likely it happens in bit of code which you didn't attach. 尝试检查堆栈跟踪以帮助您检测错误,很有可能发生在未附加的代码中。 I think place where you invoking removeEdges() might cause some issues 我认为您在调用removeEdges()可能会导致一些问题

second thing, in your Point.equal() method, could you explain me why you are casting point to cell? 第二件事,在您的Point.equal()方法中,您能否解释一下为什么将点投射到单元格?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM