我想用dijkstra的算法打印出從一個頂點到另一個頂點的路徑。當我發送code2作爲參數到列表路徑= ...我得到一個錯誤getShortestPathTo說:「Digraph類型的方法getShortestPathTo(Digraph.Vertex)不適用於參數(字符串)。我該如何施展它?它將接受一個String鑄造成一個字符串
System.out.print("City Code: ");
String code = kb.next();
String code2 = kb.next();
Digraph.computePaths(Digraph.getCode(source,code));
List<Digraph.Vertex> path = (Digraph.getShortestPathTo(code2));
這是我getShortestPathTo方法:
public static List<Vertex> getShortestPathTo(Vertex target) {
List<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous) {
path.add(vertex);
}
Collections.reverse(path);
return path;
}
請發佈頂點代碼 –
什麼是變量kb? – HyperNeutrino
@ freakshow1217我剛發佈了它。 – Kay