通过调用方法创建ArrayList,该方法在循环遍历map

时间:2015-11-19 02:52:40

标签: java loops arraylist graph

//add the edges for each vertex
    for(Map.Entry<Vertex,ArrayList<Vertex>> entry : mGraph.entrySet()) {
        Vertex key = entry.getKey();
        //System.out.println(key);
        ArrayList<Vertex> temp2  = createAdj(key);

        System.out.println(temp2);
}

//method that returns list of adjcencies for nodes
public ArrayList<Vertex> createAdj(Vertex v){

    temp = new ArrayList<Vertex>();
    char oppositeColor = flipColor(v.color);

    //Nodes that point south
    if(v.direction == "S"){
        if(v.row != 7){

            for(Vertex i: storage){

                if(i.col == v.col && i.row > v.row && i.color == oppositeColor){

                    temp.add(i);

                }

            }
        }
    }


    //node that point North
    if(v.direction == "N"){
        if(v.row != 1){

            for(Vertex i: storage){

                if(i.col == v.col && i.row < v.row && i.color == oppositeColor){

                    temp.add(i);

                }

            }
        }
    }


    //node that point West
    if(v.direction == "W"){
        if(v.col != 1){

            for(Vertex i: storage){

                if(i.row == v.row && i.col < v.col && i.color == oppositeColor){

                    temp.add(i);

                }

            }
        }
    }


    //node that point East
    if(v.direction == "E"){
        if(v.col != 7){

            for(Vertex i: storage){

                if(i.row == v.row && i.col > v.col && i.color == oppositeColor){

                    temp.add(i);

                }

            }
        }
    }

    //node that points southeast
    if(v.direction == "SE"){

        if(v.row != 7 ||v.col != 7){

            int tempRow = v.row;
            int tempColumn = v.col;

            while(tempRow < 7 && tempColumn < 7){

                tempRow++;
                tempColumn++;

                for(Vertex i: storage){

                    if(i.row == tempRow && i.col == tempColumn && i.color == oppositeColor)
                        temp.add(i);

                }
            }

        }

    }


    //node that points northeast
    if(v.direction == "NE"){

        if(v.row != 1 ||v.col != 7){

            int tempRow = v.row;
            int tempColumn = v.col;

            while(tempRow != 1 && tempColumn != 7){

                tempRow--;
                tempColumn++;

                for(Vertex i: storage){

                    if(i.row == tempRow && i.col == tempColumn && i.color == oppositeColor)
                        temp.add(i);

                }
            }

        }

    }


    //node that points southwest
    if(v.direction == "SW"){

        if(v.row != 7 ||v.col != 1){

            int tempRow = v.row;
            int tempColumn = v.col;

            while(tempRow != 7 && tempColumn != 1){

                tempRow++;
                tempColumn--;

                for(Vertex i: storage){

                    if(i.row == tempRow && i.col == tempColumn && i.color == oppositeColor)
                        temp.add(i);

                }
            }

        }

    }


    //node that points northwest
    if(v.direction == "NW"){

        if(v.row != 1 ||v.col != 1){

            int tempRow = v.row;
            int tempColumn = v.col;

            while(tempRow > 1 && tempColumn > 1){

                tempRow--;
                tempColumn--;

                for(Vertex i: storage){

                    if(i.row == tempRow && i.col == tempColumn && i.color == oppositeColor)
                        temp.add(i);

                }
            }

        }

    }

    return temp;
}


public class Vertex  {

    //name of vertex and a pointer to the first node in its adj linked list

    public int row;
    public int col;
    public char color;
    public String direction;
    public String isCircle;


    public Vertex(int row, int col, char color, String isCircle, String direction) {
        super();
        this.row = row;
        this.col = col;
        this.color = color;
        this.isCircle = isCircle;
        this.direction = direction;
    }


    @Override
    public String toString() {
        return "Vertex [row=" + row + ", col=" + col + ", color=" + color
                + ", direction=" + direction + ", isCircle=" + isCircle + "]";
    }
}

我正在使用邻接列表创建有向图。第一个框中的循环是扫描我之前创建的地图中的键,这些是需要相应邻接关系的节点。但是,当我尝试在创建的邻接上执行系统时,并非所有这些都被创建。 createAdj()方法主要是很多代码冗余,但我已经对该方法进行了大量测试,我确信每次将Vertex对象放入其中时它都会返回正确的列表。但它在上面的for循环中不起作用。此外,我确信for循环正确地循环遍历地图中的键。上面代码的输出是,没有额外的空格:

[]
[]
[]
[Vertex [row=1, col=4, color=R, direction=NW, isCircle=N], Vertex [row=1, col=5, color=R, direction=S, isCircle=N]]
[]
[Vertex [row=5, col=7, color=R, direction=NE, isCircle=N]]

不确定为什么会创建一些列表而其他列表也不会创建。

1 个答案:

答案 0 :(得分:0)

正在创建所有列表,这就是为什么它们打印为[]而不是null的原因。它们只是没有相邻的顶点被添加到列表中。但我注意到,您不是将生成的列表设置回图表中的值。

entry.setValue(temp2);

如果您使用的是Java8,则可以使用computeIfAbsent在地图中生成值:

Map<Vertex, List<Vertex>> mGraph = new ConcurrentHashMap<Vertex,List<Vertex>>();

for (Vertex vertex : mGraph.keySet()){
    List<Vertex> adjacentVertices = mGraph.computeIfAbsent(vertex, this::createAdj);
    //adjacentVertices now contains my initialized list, so do something
}

另一个主要罪恶:您正在使用==进行字符串比较。你不应该。而不是:

someString == "This"

你应该做

"This".equals(someString);
"This".equalsIgnoreCase(someString);

因此if(v.direction == "S"){代替if("S".equals(v.direction))而不是==。使用==无法保证正常运行。 .equals()运算符测试引用相等性(两个事物在内存中是否真的是同一个对象,当它们中的一个是文字时它们不会是它们),而.equals()测试VALUE相等。并且在您的文字中调用NullPointerException作为首选项,因为您知道您的文字永远不会为空,因此无法抛出"S".equals(someString)。当someStringnull时,someString.equals("S")为false,但NullPointerException会引发sudo iptables -L -n -v -x | grep -i accept