我正在尝试为邻接矩阵实现 Dijkstra 算法,并且我正在使用 Java 优先级队列来执行此操作。对于我的顶点,我正在创建一个自定义比较器类,但出现以下错误:
<anonymous Dijkstra$1> is not abstract and does not override abstract method compare(vertex,vertex) in java.util.Comparator
PriorityQueue<vertex> Q = new PriorityQueue<vertex>(n,new Comparator<vertex>() {
这是代码:
import java.util.Scanner;
import java.io.File;
import java.util.PriorityQueue;
import java.util.Comparator;
class vertex {
int v,d;
public vertex(int num,int dis){
v =num;
d=dis;
}
public int getv(){
return v;
}
public int getd(){
return d;
}
}
然后我用它来创建一个新的优先级队列:
PriorityQueue<vertex> Q = new PriorityQueue<vertex>(n,new Comparator<vertex>() {
public int compare (Object a, Object b){
vertex v1 = (vertex)a;
vertex v2 = (vertex)b;
if (v1.getd() > v2.getd()){
return +1;
}else if (v1.getd() < v2.getd()){
return -1;
}else {
return 0;
}
}});