ArrayList的作为的ArrayList关系的重新presentation [英] Arraylists of arraylist as a representation of relationships

查看:569
本文介绍了ArrayList的作为的ArrayList关系的重新presentation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个值,如:(一排元素是在关系)。

I have several values, like this: (Elements in a row are in relationship.)

       Vertex relationships(edges)
    Source vertex   Destination vertex

    x1 26   y1 287   x2 154   y2 303
    x1 22   y1 114   x2 115   y2 185
    x1 26   y1 287   x2 375   y2 338
    x1 26   y1 287   x2 260   y2 393
    x1 115  y1 185   x2 121   y2 7
    x1 200  y1 101   x2 392   y2 238
    x1 99   y1 394   x2 375   y2 338
    x1 99   y1 394   x2 121   y2 7
    x1 274  y1 28    x2 22    y2 114
    x1 296  y1 185   x2 200   y2 101
    x1 115  y1 185   x2 154   y2 303

我要找到所有这些都是关系中的值,并把它们放入一个列表,像这样: [26287 154303 375338 260393]
我试图用这个code:

I should find all the values which are in relationship and put them into a list, like this: [26,287 154,303 375,338 260,393] I have tried to use this code:

    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);
                        }
                    }
                }
            }
        }
    }

它只创建一个ArrayList中,它给了一排,而不是单独的所有元素。我曾尝试调试,但我看不出是什么原因造成这一点。

It creates only one ArrayList, it gives all elements in a row instead of separately. I have tried debugging, but I can not see what causes this.

我想要的例子:

Example of what I want:

推荐答案

我要在这三个步骤:定义数据结构,定义问题,提供了解决方案。

I'll take this in three steps: define the data structures, define the problem, provide a solution.


  • 顶点:在你的榜样顶点似乎是唯一的一对整数。 应该是pretty好配合

  • 关系:这似乎是由两个顶点​​所定义的边缘。你应该写这一个简单的POJO,但为了简便起见,我们将使用<一个href=\"http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/tuple/Pair.html\"相对=nofollow>对来自Apache公地。让我们宣布这个关系从右向左走。因此,对&LT;点对点,&GT;关系=新ImmutablePair&LT;点对点,&GT;(新点(26,287),新点(154,303)); 等同于您的示例数据的第一行

  • Vertex: in your example a vertex seems to be a unique pair of integers. Point should be a pretty good fit
  • Relationship: This seems to be an edge defined by two Vertexes. You should write a simple pojo for this, but for the sake of brevity we'll use Pair from apache commons. Lets declare that the Relationships go from right to left. Thus Pair<Point, Point> relationship = new ImmutablePair<Point, Point>(new Point(26, 287), new Point(154, 303)); is equivalent to the first line in your example data.

您想要一个发生在关系的列表,并吐出了展示,人们可以从任何给定的顶点到列表的列表的方法。我买了一个另外的并与点作为键套可能的点作为值返回地图。 IE浏览器。 地图&LT;点,集&LT;点和GT;&GT;

You want a method that takes in a list of Relationships and spits out a list of lists showing where one can get to from any given Vertex. I'll take it one further and return map with from points as keys and sets of possible to points as values. ie. Map<Point,Set<Point>>

此时背景明确,找到一个解决方案是容易

At this point the background is clearly defined and finding a solution is easy

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;
}

注意,我没有以任何方式测试了这个

这篇关于ArrayList的作为的ArrayList关系的重新presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆