内部类对象的 PriorityQueue - 找不到构造函数
我需要一个对象的优先级队列,但我不断收到此错误:
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>()
这是我的代码摘录:
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();
}
});
}
有什么想法吗?我是否需要将 Node 类公开并将其放入自己的文件中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试使用不存在的构造函数来创建 PriorityQueue 对象。 JavaDoc 中没有定义 PriorityQueue(Comparator) 构造函数 (http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html)。它确实有一个需要一个 int 来表示initialCapacity 和一个比较器,你可能想尝试一下。
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.
问题是没有
PriorityQueue(Comparator) 类型的构造函数
您可以获得的最接近的一个是
PriorityQueue(int InitialSize, Comparator)
。请参阅 此处只需将 main 更改为:
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 hereJust change your main to :