Search code examples
javaobjectqueuepriority-queuecomparable

Priority Queue cannot be applied to given types


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

Solution

  • The SLD is not the Comparator, it is just the thing being compared. You need to create a class that does the actual comparison, and submit that:

    new Comparator<city>() {
        @Override
        public int compare(city city1, city city2) {
            return city1.getSLD() - city2.getSLD();
        }
    };
    

    Read up on java.util.Comparator to get more acquainted.

    • Edit -

    This is just one error, though. The error you're getting from compilation, as described in the output from the compiler, is due to the fact that you're using the wrong arguments for the offer() method of PriorityQueue. Only a city should be passed: The SLD will be handled by the code of your Comparator instance.