首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在打印源和目标的每一条边,但是当我在mxgraph java中使用getEdgeCount()时,它们是重复的

我正在打印源和目标的每一条边,但是当我在mxgraph java中使用getEdgeCount()时,它们是重复的
EN

Stack Overflow用户
提问于 2021-05-05 11:09:29
回答 1查看 39关注 0票数 0

我使用下面的函数来显示图中的每条边及其源和目标。然而,我注意到一些边缘不知从哪里冒出来。

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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

我所期望的是:

代码语言:javascript
复制
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

有没有解决这个问题的办法?

EN

回答 1

Stack Overflow用户

发布于 2021-05-05 12:24:49

现在我知道我的代码的问题所在了。方法getEdgesAt(int index)返回所有的边,无论它们是传入的还是传出的,为了让我的方法工作,我只需要传出的边。我将该方法替换为:

代码语言:javascript
复制
    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());
            }
            
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67394698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档