问题描述
我正在使用以下函数向我显示图中的每条边及其源和目标。但是,我注意到一些边缘突然出现。
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
有没有办法解决这个问题?
解决方法
现在我知道我的代码有问题了。 方法 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());
}
}
}