我是新的與優先級隊列工作,並已格式化此代碼錯誤,我想我的優先級是一個城市的直線距離,但我不相信我通過這個信息正確地排入隊列。綜觀API我需要設置SLD作爲比較優先級隊列不能應用於給定的類型
公共的PriorityQueue(INT參數:initialCapacity,比較器比較器)
創建一個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
'if(current.getname()==「Bucharest」)'不要將字符串與'=='進行比較。改用'.equals()'。最佳做法是像這樣使用它:''stringLiteral「.equals(stringVariable)''因爲你可以避免產生NullPointerException。 – RaptorDotCpp
'city city,int SLD'是如何聲明參數的,而'新的PriorityQueue(城市城市,int SLD)'是構造函數調用(未定義)。它應該是'新的PriorityQueue (someInteger,anInstanceOfCaparator)'。參數'someInteger,anInstanceOfCaparator'必須是構造函數簽名所要求的類型。 –