[英]Arraylists of arraylist as a representation of relationships
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
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: 我应该找到关系中的所有值并将它们放入列表中,如下所示:
[26,287 154,303 375,338 260,393]
我试图使用此代码:
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);
}
}
}
}
}
}
It creates only one ArrayList, it gives all elements in a row instead of separately. 它只创建一个ArrayList,它提供一行中的所有元素而不是单独的。 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. 我将分三步:定义数据结构,定义问题,提供解决方案。
Pair<Point, Point> relationship = new ImmutablePair<Point, Point>(new Point(26, 287), new Point(154, 303));
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. 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;
}
Note, I have not tested this in any way 注意,我没有以任何方式测试过这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.