为什么列出<linkedlist <string>&gt;不工作?</linkedlist <string>

时间:2013-10-01 18:04:37

标签: java list linked-list

我遇到List<LinkedList<String>>的问题。 printPath方法正在打印字符串的链接列表,但它不会进入favp。 请帮我。任何线索,链接,参考将是可观的。 谢谢。

private List<LinkedList<String>> favp = new ArrayList<LinkedList<String>>();
 private void printPath(LinkedList<String> visited) {
    favp.add(visited);
    for (String node : visited) {
        System.out.print(node);
        System.out.print(" ");
    }
    System.out.println();
}

private void printFavp() {
    for (LinkedList<String> ls : favp) {
        for (String s : ls) {
            System.out.print("-->" + s);
        }
        System.out.println();
    }
}

编辑: visit是String的链接列表。 我通过printPath()方法在控制台上打印它。 并排我试图将字符串的链接列表visited放入favp。 我已经制作了printFavp()方法来打印它中的值。 谢谢。

编辑2: 这是Graph.java

package search;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Search {

    private static final String START = "C1";
    private static final String END = "C5";
    private static final int STOP = 5;
    private List<LinkedList<String>> favp = new ArrayList<LinkedList<String>>();

    public static void main(String[] args) {
        // this graph is directional
        Graph graph = new Graph();
        graph.addEdge("C1", "C2");
        graph.addEdge("C1", "C4");
        graph.addEdge("C2", "C3");
        graph.addEdge("C3", "C1");
        graph.addEdge("C1", "C5");
        graph.addEdge("C3", "C4");
        graph.addEdge("C3", "C5");
        graph.addEdge("C4", "C5");
        graph.addEdge("C5", "C2");
        LinkedList<String> visited = new LinkedList<String>();
        visited.add(START);
        Search s = new Search();
        s.breadthFirst(graph, visited);
        s.printFavp();
    }

    private void breadthFirst(Graph graph, LinkedList<String> visited) {
        LinkedList<String> nodes = graph.adjacentNodes(visited.getLast());
        // examine adjacent nodes
        for (String node : nodes) {
            if (visited.contains(node)) {
                continue;
            }
            if (node.equals(END)) {
                visited.add(node);
                System.out.print("----->");
                printPath(visited);
                visited.removeLast();
                break;
            }
        }
        // in breadth-first, recursion needs to come after visiting adjacent
        // nodes
        for (String node : nodes) {
            if (visited.contains(node) || node.equals(END)) {
                continue;
            }
            visited.addLast(node);
            int stop = STOP + 2;
            if (visited.size() < stop)
                breadthFirst(graph, visited);
            visited.removeLast();
        }
    }

    private void printPath(LinkedList<String> visited) {
        favp.add(visited);
        for (String node : visited) {
            System.out.print(node);
            System.out.print(" ");
        }
        System.out.println();
    }

    private void printFavp() {
        for (LinkedList<String> ls : favp) {
            for (String s : ls) {
                System.out.print("-->" + s);
            }
            System.out.println();
        }
    }
}

这是Search.java

package search;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class Graph {
    private Map<String, LinkedHashSet<String>> map = new HashMap<String, LinkedHashSet<String>>();

    public void addEdge(String node1, String node2) {
        LinkedHashSet<String> adjacent = map.get(node1);
        if(adjacent==null) {
            adjacent = new LinkedHashSet<String>();
            map.put(node1, adjacent);
        }
        adjacent.add(node2);
    }

    public void addTwoWayVertex(String node1, String node2) {
        addEdge(node1, node2);
        addEdge(node2, node1);
    }

    public boolean isConnected(String node1, String node2) {
        Set<?> adjacent = map.get(node1);
        if(adjacent==null) {
            return false;
        }
        return adjacent.contains(node2);
    }

    public LinkedList<String> adjacentNodes(String last) {
        LinkedHashSet<String> adjacent = map.get(last);
        if(adjacent==null) {
            return new LinkedList<String>();
        }
        return new LinkedList<String>(adjacent);
    }
}

1 个答案:

答案 0 :(得分:0)

当您将访问添加到favp时,您正在推送访问列表本身,因此如果您在printPath调用之外修改它,它将会更改。添加到favp:

时尝试克隆访问
favp.add(visited.clone());