简体   繁体   English

Dijkstra的最短路径算法Lars Vogel

[英]Dijkstra's shortest path algorithm Lars Vogel

I'm trying to implement Dijkstra algorithm in java from the site of Lars Vogel : 我正试图在Lars Vogel网站上用java实现Dijkstra算法:
http://www.vogella.com/articles/JavaAlgorithmsDijkstra/article.html . http://www.vogella.com/articles/JavaAlgorithmsDijkstra/article.html
But there is no main function and when I create one public static void it gives me errors that non-static variables or classes cannot be referenced from static context. 但是没有主要功能,当我创建一个公共静态void时,它给出了错误,即非静态变量或类不能从静态上下文中引用。
Do I have to make all the classes static or there is another 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<>();
    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();
  }
}

Run it directly with this code: 使用以下代码直接运行它:

public static void main() {
    new TestDijkstraAlgorithm().testExcute();
}

You have to create an instance of your class first. 您必须先创建一个类的实例。 main method is always static, so you can't call instance methods (non-static) directly. main方法总是静态的,因此不能直接调用实例方法(非静态)。 To create an instance, just call the constructor with new TestDijkstraAlgorithm() . 要创建实例,只需使用new TestDijkstraAlgorithm()调用构造函数。 There is no constructor defined explicitly, so the default one, without parameters, is automatically available. 没有明确定义构造函数,因此默认值(不带参数)可自动使用。

These are OOP basics, you should really read into it. 这些都是OOP的基础知识,你应该真正地阅读它。

That being said, the supposed way to call the testExecute method is with JUnit . 话虽这么说,调用testExecute方法的testExecute方法是使用JUnit That's why there is the @Test annotation. 这就是@Test注释的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM