Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        6年前关闭。
                    
                
        

我在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();
    }
}


编辑:
访问的是String的链接列表。
我正在通过printPath()方法在控制台上打印它。
我正在并排尝试将String的链接列表即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);
    }
}

最佳答案

将访问者添加到favp时,您将推动访问者列表本身,因此,如果在printPath调用之外对其进行修改,则它将更改。尝试在添加到favp时克隆访问的站点:

favp.add(visited.clone());

07-28 14:13