Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 作为关系表示的arraylist的arraylist_Java_Arraylist_Graph Algorithm - Fatal编程技术网

Java 作为关系表示的arraylist的arraylist

Java 作为关系表示的arraylist的arraylist,java,arraylist,graph-algorithm,Java,Arraylist,Graph Algorithm,我有几个值,如下所示:(行中的元素是相互关联的。) 我应该找到所有相关的值并将它们放入一个列表中,如下所示:[26287154303375338260393] 我已尝试使用此代码: for (int i=0; i<vertexnum; i++) { adjLists.add(new ArrayList<Integer>()); } for (int j=0; j<vertexnum; j++) { for (Poi

我有几个值,如下所示:(行中的元素是相互关联的。)

我应该找到所有相关的值并将它们放入一个列表中,如下所示:
[26287154303375338260393]
我已尝试使用此代码:

    for (int i=0; i<vertexnum; i++) {
        adjLists.add(new ArrayList<Integer>());
    }

    for (int j=0; j<vertexnum; j++) {
        for (Point p : nodes) {
            for (Edge e : edges) {
                adjLists.get(j).add(e.p1.x);
                adjLists.get(j).add(e.p1.y);
                adjLists.get(j).add(0);

                adjLists.get(j).add(e.p2.x);
                adjLists.get(j).add(e.p2.y);
                adjLists.get(j).add(0);
                for (Point p1 : nodes) {
                    for (Edge e1 : edges) {
                        if (e1.p1.x == e.p1.x && e1.p1.y == e.p1.y && !adjLists.get(j).contains(e1.p2.x) && !adjLists.get(j).contains(e1.p2.y)) {
                            adjLists.get(j).add(e1.p2.x);
                            adjLists.get(j).add(e1.p2.y);
                            adjLists.get(j).add(0);
                        } else if(e1.p2.x == e.p1.x && e1.p2.y == e.p1.y && !adjLists.contains(e1.p1.x) && !adjLists.contains(e1.p1.y)){
                            adjLists.get(j).add(e1.p1.x);
                            adjLists.get(j).add(e1.p1.y);
                            adjLists.get(j).add(0);
                        }
                    }
                }
            }
        }
    }

for(inti=0;i我将分三步进行:定义数据结构,定义问题,提供解决方案

定义数据结构
  • 顶点:在你的例子中,一个顶点似乎是一对唯一的整数。应该是非常合适的
  • 关系:这似乎是由两个顶点定义的边。您应该为此编写一个简单的pojo,但为了简洁起见,我们将使用apache commons。让我们声明关系从右到左。因此
    Pair Relationship=new ImmutablePair(新点(26287)、新点(154303));
    相当于示例数据中的第一行
定义问题 您需要一个方法,该方法接收一个关系列表,并输出一个列表,显示从任何给定顶点可以到达的位置。我将进一步使用该方法,并返回以from点为键的贴图,以及以可能的to点集为值的贴图。即
map

解决方案 在这一点上,背景是明确的,找到一个解决方案是很容易的

public static Map<Point, Set<Point>> createTraversalMap(List<Pair<Point, Point>> relationshipList) {
    Map<Point, Set<Point>> traversalMap = new HashMap<Point, Set<Point>>();
    for (Pair<Point, Point> relationship : relationshipList) {
        Point fromVertex = relationship.getLeft(), toVertex = relationship.getRight();
        Set<Point> toSet = traversalMap.get(fromVertex);// set of Vertexes we've found so far for the current "from" Vertex
        if (toSet == null) {// bootstrap the set
            toSet = new HashSet<Point>();
            traversalMap.put(fromVertex, toSet);
        }
        toSet.add(toVertex);
        // traversalMap.put(fromVertex, toSet); //not needed, but good to keep in mind
    }
    return traversalMap;
}
public静态映射createTraversalMap(列表关系ShipList){
Map traversalMap=新HashMap();
for(配对关系:relationshipList){
点fromVertex=relationship.getLeft(),toVertex=relationship.getRight();
Set toSet=traversalMap.get(fromVertex);//到目前为止,我们为当前“from”顶点找到的顶点集
如果(toSet==null){//引导集合
toSet=newhashset();
遍历映射放置(从顶点到集合);
}
toSet.add(toVertex);
//traversalMap.put(fromVertex,toSet);//不需要,但最好记住
}
返回遍历ALMAP;
}

注意,我没有以任何方式对此进行测试

您能发布您得到的输出和您期望的输出吗?所有顶点的预期输出都是这样的(x-y坐标表示顶点):[26287 154303 375338 260393][22114 115185 274,28]此代码提供了一个包含所有上述坐标的列表。它仅“连接”它们。为什么要循环<代码>点p:节点< /代码>?我不能看到在循环中使用代码< >代码>代码。是的,没有必要。抱歉!@ MUM00 7,如果循环不必要,请更新您的帖子中的代码以不包括它。否则很难理解正在发生的事情。还要考虑添加<代码> Soal.Out.Prtutl。n
声明,明确您正在查看的数据,并在问题中包含预期结果。
public static Map<Point, Set<Point>> createTraversalMap(List<Pair<Point, Point>> relationshipList) {
    Map<Point, Set<Point>> traversalMap = new HashMap<Point, Set<Point>>();
    for (Pair<Point, Point> relationship : relationshipList) {
        Point fromVertex = relationship.getLeft(), toVertex = relationship.getRight();
        Set<Point> toSet = traversalMap.get(fromVertex);// set of Vertexes we've found so far for the current "from" Vertex
        if (toSet == null) {// bootstrap the set
            toSet = new HashSet<Point>();
            traversalMap.put(fromVertex, toSet);
        }
        toSet.add(toVertex);
        // traversalMap.put(fromVertex, toSet); //not needed, but good to keep in mind
    }
    return traversalMap;
}