graphstream/gs-core

Removing an Edge - ArrayIndexOutOfBoundsException

Runster opened this issue · 3 comments

Hey!

The following code ends up in an Exception:

` Graph graph = new SingleGraph("example");

	graph.addNode("a");
	graph.addNode("b");
	graph.addEdge("ab", "a", "b");
	
	Edge edge = graph.getEdge("ab");
	
	Graph tempGraph = Graphs.clone(graph);
	
	tempGraph.removeEdge(edge);
	graph.removeEdge(edge);`

Seems like he noticed in the original Graph, that the edge has been removed. My workaround for this problem is to remove the Id (String) of the edge, not the object itself.

You need to use the Id of the edge, not the object itself.

graph.addNode("a");
graph.addNode("b");
graph.addEdge("ab", "a", "b");
	
Edge edge = graph.getEdge("ab");

Graph tempGraph = Graphs.clone(graph);
	
tempGraph.removeEdge(edge.getId());
graph.removeEdge(edge.getId());`

Yes of course, but in this case the wrong object is used. He used graph.getEdge("ab"); to get the object "ab" from "graph", but when "graph" is cloned, "ab" got cloned as well. Thus, you can either use the id to identify "ab", or get the object from the new graph:

Edge edge = graph.getEdge("ab");

Graph tempGraph = Graphs.clone(graph);

Edge edgeClone = tempGraph.getEdge("ab");

tempGraph.removeEdge(edge);
graph.removeEdge(edgeClone);`