Arraylists of arraylist as a representation of relationships/adjacency in graphs

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

[quote]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
[/quote]
I should find all the values which are in relationship and put them into a list, like this: [quote][26,287 154,303 375,338 260,393]
[/quote]
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);
                        }
                    }
                }

            }
        }
    }

It creates only one arraylist, it gives all elements in a row intead of separating. I have tried debugging, but I can not see what causes this.
So as you see above example I should build, create a neighbour chain, but above code only work for the first vertex. It cannot creates new list for each vertex and for his neighbours.

You have a typo in your else if branch:


else if(e1.p2.x == e.p1.x && e1.p2.y == e.p1.y && !adjLists.contains(e1.p1.x) && !adjLists.contains(e1.p1.y))

should probably be


else if(e1.p2.x == e.p1.x && e1.p2.y == e.p1.y && !adjLists.get(j).contains(e1.p1.x) && !adjLists.get(j).contains(e1.p1.y))

Overall it’s easier to read and more performant to store adjLists.get(j) into a variable anyway:


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

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

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

                            } else if(e1.p2.x == e.p1.x && e1.p2.y == e.p1.y && !aList.contains(e1.p1.x) && !aList.contains(e1.p1.y)){
                                aList.add(e1.p1.x);
                                aList.add(e1.p1.y);
                                aList.add(0);
                            }
                        }
                    }

                }
            }
        }

It should make separated lists for every vertex.
Like these: [26,287 154,303 375,338 260,393] [22,114 115,185 274,28]
Above code gives only 1 list.