如何使用新的比较器和NO指定的初始容量创建PriorityQueue?

前端之家收集整理的这篇文章主要介绍了如何使用新的比较器和NO指定的初始容量创建PriorityQueue?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@
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.

原文链接:https://www.f2er.com/java/121256.html

猜你在找的Java相关文章