我使用下面的函数来显示图中的每条边及其源和目标。然而,我注意到一些边缘不知从哪里冒出来。
public void showEdges()
{
Object[] list = graph.getChildVertices(graph.getDefaultParent());
for(int i=0; i< list.length; i++)
{
mxICell vertex = (mxICell) list[i];
for (int j =0; j<vertex.getEdgeCount(); j++)
{
mxICell edge = vertex.getEdgeAt(j);
String destination =(String) (edge.getTerminal(false)).getValue();
System.out.println("vertex-" + i + " is connected to " +
destination + " with weight " + (String)edge.getValue());
}
}
}我画的是这个链接:https://pin.it/7b0Qh7v
输出:
vertex-0 is connected to 0 with weight 2
vertex-0 is connected to 2 with weight 15
vertex-1 is connected to 2 with weight 17
vertex-2 is connected to 2 with weight 17
vertex-2 is connected to 2 with weight 15我所期望的是:
vertex-0 is connected to 0 with weight 2
vertex-0 is connected to 2 with weight 15
vertex-1 is connected to 2 with weight 17有没有解决这个问题的办法?
发布于 2021-05-05 12:24:49
现在我知道我的代码的问题所在了。方法getEdgesAt(int index)返回所有的边,无论它们是传入的还是传出的,为了让我的方法工作,我只需要传出的边。我将该方法替换为:
public void getEdges()
{
Object[] list = graph.getChildVertices(graph.getDefaultParent());
for(int i=0; i< list.length; i++)
{
mxICell vertex = (mxICell) list[i];
Object[] edges = graph.getEdges(vertex, graph.getDefaultParent(), false, true, true);
for (int j =0; j<edges.length; j++)
{
String destination =(String) (((mxICell)edges[j]).getTerminal(false)).getValue();
System.out.println("vertex-" + i + " is connected to " +
destination + " with weight " + (String)edge.getValue());
}
}
}https://stackoverflow.com/questions/67394698
复制相似问题