簡體   English   中英

刪除所有接觸給定頂點的邊

[英]Remove all edges touching a given vertex

因此,我試圖刪除SimpleGraph(無向圖,JGraphT)的所有邊緣,但是由於某些原因,我一直在獲取ConcurrentModificationException。

這是我想做的事情:

首先,我有一個類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的頂點是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?

ConCurrentModificationException表示您正在嘗試修改項目,為什么不允許這樣做,即在迭代時嘗試修改Collection時發生

嘗試檢查堆棧跟蹤以幫助您檢測錯誤,很有可能發生在未附加的代碼中。 我認為您在調用removeEdges()可能會導致一些問題

第二件事,在您的Point.equal()方法中,您能否解釋一下為什么將點投射到單元格?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM