在
Java中,我不知道如何用新的比较器创建一个新的PriorityQueue但没有给出队列长度?我该如何创建它?
我知道我可以写:
Queue<Node> theQueue = new PriorityQueue<Node>(15,new Comparator<Node>();
但我希望队列可以像LinkedList一样工作,我的意思是它的长度不固定,我怎么能声明呢?
解决方法
没有这样的构造函数.根据JavaDocs,the default capacity is 11,您可以指定与无参数PriorityQueue构造函数的类似行为:
Queue<Node> theQueue = new PriorityQueue<Node>(11,new Comparator<Node>());
是的,the queue will grow if it needs to.
A priority queue is unbounded,but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue,its capacity grows automatically. The details of the growth policy are not specified.