我有问题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();
}
}
编辑:visited 是一个字符串的链表。我通过 printPath() 方法在控制台上打印它。并排我试图将 String ie 的链表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);
}
}