0

我需要对象的优先级队列,但我不断收到此错误:

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 类并将其放入它自己的文件中?

4

2 回答 2

1

您尝试使用不存在的构造函数来创建 PriorityQueue 对象。JavaDoc ( http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html )中没有定义 PriorityQueue(Comparator) 构造函数。它确实有一个将 int 用于 initialCapacity 和一个比较器,您可能想尝试一下。

于 2010-09-01T00:46:00.240 回答
0

问题是没有类型的构造函数PriorityQueue<E>(Comparator<E>) 你能得到的最接近的是PriorityQueue<E>(int InitialSize, Comparator<E>).See here

只需将您的主要更改为:

  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(); 
       }
      });
于 2013-02-02T12:59:42.600 回答