I am new with working with priority queues and have formatted this code wrong, and I want to have my priority be a straight line distance to a city however I don't beleive I passed this information to the queue correctly. Looking at the API I need to set the SLD as a the comparator
public PriorityQueue(int initialCapacity, Comparator comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
but this isn't clear to me.
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");
}
My error is:
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")Don't compare Strings with '=='. Use.equals()instead. Best practice is using it like this:"stringLiteral".equals(stringVariable)because you can avoid having a NullPointerException. - RaptorDotCppcity city,int SLDis how parameters are declared, whilenew PriorityQueue<city>(city city,int SLD)is constructor invocation (not definition). It should benew PriorityQueue<city>(someInteger, anInstanceOfCaparator). The argurmentssomeInteger, anInstanceOfCaparatormust be of types as required by the constructor signature. - Bhesh Gurung