java - Dijkstra's shortest path algorithm Lars Vogel -
i'm trying implement dijkstra algorithm in java site of lars vogel :
http://www.vogella.com/articles/javaalgorithmsdijkstra/article.html.
there no main function , when create 1 public static void gives me errors non-static variables or classes cannot referenced static context.
have make classes static or there solution?
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<>(); (int = 0; < 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 location loc_1 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); (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(); } }
run directly code:
public static void main() { new testdijkstraalgorithm().testexcute(); }
you have create instance of class first. main
method static, can't call instance methods (non-static) directly. create instance, call constructor new testdijkstraalgorithm()
. there no constructor defined explicitly, default one, without parameters, automatically available.
these oop basics, should read it.
that being said, supposed way call testexecute
method junit. that's why there @test
annotation.
Comments
Post a Comment