[英]Constructor error with given type, cannot be applied
我正在使用Dijkstras算法,但似乎无法弄清楚为什么我的构造函数无法正确地执行我想做的事情。
具体来说,这行代码:A.edges = new Edge [] {new Edge(B,35),new Edge(C,50)};
给我一个错误:“错误:类Edge中的构造函数Edge无法应用于给定类型;”
public static void main(String[]args){
//sets all the cities/nodes to a vertex
Vertex A = new Vertex("CityA");
Vertex B = new Vertex("CityB");
//distance from each city to their new cities
A.edges = new Edge[]{ new Edge(B, 35), new Edge(C, 50)};
}
public static void dijkstra(Vertex s){
s.shortestDist = 0;
PriorityQueue<Vertex> cityQueue = new PriorityQueue<Vertex>();
cityQueue.add(s);
while(!cityQueue.isEmpty()){
Vertex w = cityQueue.poll();
for (Edge x : w.edges){
Vertex v = x.city;
int price = x.price;
int priceOfTrip = w.shortestDist + price;
if(priceOfTrip < v.shortestDist){ //relaxes the edge that it's on
cityQueue.remove(v);
v.shortestDist = priceOfTrip;
v.prev = w;
cityQueue.add(v);
}
}
}
}
//Contructor
public static class Edge{
public static int price;
public static Vertex city;
public static void Edge(Vertex altCity, int altPrice){
city = altCity;
price = altPrice;
}
}
这条线
public static void Edge(Vertex altCity, int altPrice){
不是构造函数; 这是一个返回void
的静态方法。 构造函数不是static
,它们不返回任何东西。 尝试
public Edge(Vertex altCity, int altPrice){
此外,您的Edge
类的成员变量也不应该是static
:
public int price;
public Vertex city;
这是一种方法语法:
public static void Edge(Vertex altCity, int altPrice){
这不是构造函数。 您的构造函数应类似于:
public Edge(Vertex altCity, int altPrice){
您可以在这里参考学习。
此外,您的字段是类变量,因此,每次调用构造函数时,都将为所有对象设置相同的变量并为其设置新值,而不确定这是否是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.