简体   繁体   中英

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.

Here's what I'm trying to do:

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

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

Can anyone tell me what I did wrong here? why do I keep getting 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

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

second thing, in your Point.equal() method, could you explain me why you are casting point to cell?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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