PriorityQueue of Objects from an Inner Class - Can't find constructor
I need a priority queue of objects but I keep getting this error:
symbol: constructor PriorityQueue(anonymous java.util.Comparator<map.Node>>)
location: class java.util.PriorityQueue<map.Node>
PriorityQueue<Node> pq = new PriorityQueue<Node>(new Comparator<Node>()
Here an excerpt from my code:
public class map {
static class Node {
Node parent;
State state;
private int cost;
public Node() {};
public Node(Node parent_passed, State state_passed, Integer cost_passed ) { 开发者_运维百科
this.parent = parent_passed;
this.state = state_passed;
this.cost = cost_passed;
}
public int getCost()
{
return cost;
}
}
public static void main(String[] args)
{
PriorityQueue<Node> pq = new PriorityQueue<Node>(new Comparator<Node>()
{
public int compare(Node a1, Node a2) {
return a2.getCost() - a1.getCost();
}
});
}
Any ideas? Do I need to make the Node class public and put it in it's own file?
You trying to use a constructor that doesn't exist to create the PriorityQueue object. There is no PriorityQueue(Comparator) constructor defined in the JavaDoc (http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html). It does have one that takes an int for initialCapacity and a comparator, you might want to try that.
The problem is that there is no constructor of the type PriorityQueue<E>(Comparator<E>)
The closest one you can get is PriorityQueue<E>(int InitialSize, Comparator<E>)
.See here
Just change your main to :
public static void main(String[] args)
{
@Override
PriorityQueue<Node> pq = new PriorityQueue<Node>(10, new Comparator<Node>()
{
public int compare(Node a1, Node a2) {
return a2.getCost() - a1.getCost();
}
});
精彩评论