加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:48:25 所属栏目:Java 来源:网络整理
导读:在 Java中,我不知道如何用新的比较器创建一个新的PriorityQueue但没有给出队列长度?我该如何创建它? 我知道我可以写: QueueNode theQueue = new PriorityQueueNode(15,new ComparatorNode(); 但我希望队列可以像LinkedList一样工作,我的意思是它的长度不
在 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读