Java 删除与给定顶点接触的所有边
因此,我试图删除SimpleGraph(无向图,JGraphT)的所有边,但出于某种原因,我一直得到ConcurrentModificationException 以下是我想做的: 首先,我有一个类点,如Fowlowed:Java 删除与给定顶点接触的所有边,java,graph,java.util.concurrent,jgrapht,Java,Graph,Java.util.concurrent,Jgrapht,因此,我试图删除SimpleGraph(无向图,JGraphT)的所有边,但出于某种原因,我一直得到ConcurrentModificationException 以下是我想做的: 首先,我有一个类点,如Fowlowed: class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } //gette
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的顶点是点的实例并存储在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);
}
Point[][]pointContainer=新点[100][100];
SimpleGraph g=新的SimpleGraph();
公共void initGraph()
{
用于(int行=0;行<100;++行)
用于(整数列=0;列<100;++列)
{
点p=新点(列,行);
pointContainer[row][col]=p;
g、 addVertex(p);
}
//然后在任何相邻顶点之间添加边
//所以除了网格边缘附近的顶点,每个顶点有8条边
}
公共无效删除(整行、整列)
{
设置边=g.edgesOf(pointContainer[row][col]);
g、 去除边缘;
}
有人能告诉我我做错了什么吗?为什么我总是收到ConCurrentModificationException?
ConCurrentModificationException
表示您正在尝试修改项目,为什么不允许它,
当您尝试在迭代集合时修改集合时,会发生ie
尝试检查堆栈跟踪以帮助您检测错误,它很可能发生在您未附加的代码位中。我认为调用removedges()
的地方可能会引起一些问题
第二件事,在您的
Point.equal()
方法中,您能解释一下为什么要将Point转换为cell吗 ConCurrentModificationException
表示您正在尝试修改项目,为什么它不被允许,
当您尝试在迭代集合时修改集合时,会发生ie
尝试检查堆栈跟踪以帮助您检测错误,它很可能发生在您未附加的代码位中。我认为调用removedges()
的地方可能会引起一些问题
第二件事,在您的
Point.equal()
方法中,您能解释一下为什么要将Point转换为cell吗 对不起,这是个打字错误。我本想把rhs投到点子上的。对不起,这是个打字错误。我想把rhs投到点子上。