我是使用优先级队列的新手,并且此代码的格式错误,我希望我的优先级是到城市的直线距离,但我不相信我正确地将这些信息传递给队列。查看 API 我需要将 SLD 设置为比较器
public PriorityQueue(int initialCapacity, Comparator compare)
创建一个具有指定初始容量的 PriorityQueue,它根据指定的比较器对其元素进行排序。
但这对我来说并不清楚。
public static void GreedySearchMap(map Romania) {
boolean done = false;
city current;
int numsteps = 10;
int cursteps;
int choice;
int numconnections;
int totaldist;
cursteps = 0;
current = Romania.Arad;
totaldist = 0;
/*create queue*/
PriorityQueue<city> q = new PriorityQueue<city>(city city,int SLD);
q.offer(current);
current.visited = true;
while (!done) {
System.out.printf("Step %d, In %s, Distance\t%d\n", cursteps,
current.getname(), totaldist);
if (current.getname() == "Bucharest")
done = true;
else {
current = q.poll();
cursteps++;
numconnections = current.getconnections();
for (int i = 0; i < numconnections; i++) {
choice = i;
if (current.getcity(choice).visited == false) {
//totaldist += current.getdist(choice);
q.offer(current.getcity(choice), current.getSLD());
current.visited = true;
}
}
}
}
System.out.printf("-----------------------\n");
}
我的错误是:
P:\csci395\hw4>javac GS.java
GS.java:85: error: method offer in class PriorityQueue<E> cannot be applied to g
iven types;
q.offer(current.
getcity(choice), current.getSLD());
^
required: city
found: city,int
reason: actual and formal argument lists differ in length
where E is a type-variable:
E extends Object declared in class PriorityQueue
1 error