나는 라스 보글의 사이트에서 자바 익스트라 알고리즘을 구현하기 위해 노력하고있어 :
http://www.vogella.com/articles/JavaAlgorithmsDijkstra/article.html합니다.
하지만 주요 기능은 없으며 public static void 하나를 만들면 정적이 아닌 변수 나 클래스를 정적 컨텍스트에서 참조 할 수없는 오류가 발생합니다.
모든 클래스를 정적으로 만들거나 다른 솔루션이 필요합니까? 이 코드를 직접다 익스트라의 최단 경로 알고리즘 라스 보글
package de.vogella.algorithms.dijkstra.test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
import de.vogella.algorithms.dijkstra.engine.DijkstraAlgorithm;
import de.vogella.algorithms.dijkstra.model.Edge;
import de.vogella.algorithms.dijkstra.model.Graph;
import de.vogella.algorithms.dijkstra.model.Vertex;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class TestDijkstraAlgorithm {
private List<Vertex> nodes;
private List<Edge> edges;
@Test
public void testExcute() {
nodes = new ArrayList<>();
edges = new ArrayList<>();
for (int i = 0; i < 11; i++) {
Vertex location = new Vertex("Node_" + i, "Node_" + i);
nodes.add(location);
}
addLane("Edge_0", 0, 1, 85);
addLane("Edge_1", 0, 2, 217);
addLane("Edge_2", 0, 4, 173);
addLane("Edge_3", 2, 6, 186);
addLane("Edge_4", 2, 7, 103);
addLane("Edge_5", 3, 7, 183);
addLane("Edge_6", 5, 8, 250);
addLane("Edge_7", 8, 9, 84);
addLane("Edge_8", 7, 9, 167);
addLane("Edge_9", 4, 9, 502);
addLane("Edge_10", 9, 10, 40);
addLane("Edge_11", 1, 10, 600);
// Lets check from location Loc_1 to Loc_10
Graph graph = new Graph(nodes, edges);
DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(graph);
dijkstra.execute(nodes.get(0));
LinkedList<Vertex> path = dijkstra.getPath(nodes.get(10));
assertNotNull(path);
assertTrue(path.size() > 0);
for (Vertex vertex : path) {
System.out.println(vertex);
}
}
private void addLane(String laneId, int sourceLocNo, int destLocNo,
int duration) {
Edge lane = new Edge(laneId,nodes.get(sourceLocNo), nodes.get(destLocNo), duration);
edges.add(lane);
}
public static void main() {
testExcute();
}
}
코드를 게시하면 도움이 될 수 있습니다. ppl이 문제를 추측하거나 전체 솔루션을 작성할 것으로 기대할 수는 없습니다. -.- – Nikki
main 메소드에서 클래스의 오브젝트를 작성한 다음 Dijkstra 메소드를 호출하십시오. – nalply
'DijkstraAlgorithm' 인스턴스를 생성해야합니다. 그리고 아마 조금 OOP를 배울 수 있습니다. –