Я новичок в работе с очередями приоритетов и неправильно отформатировал этот код, и я хочу, чтобы мой приоритет был прямым расстоянием до города, но я не верю, что я прошел мимо этого информация в очередь правильно. Глядя на 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' - как объявляются параметры, а' new PriorityQueue (city city, int SLD) '- вызов конструктора (не определение). Он должен быть 'new PriorityQueue (someInteger, anInstanceOfCaparator)'. Аргументы 'someInteger, anInstanceOfCaparator' должны быть типов, которые требуются подписи конструктора. –