微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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.jb51.cc/java/121256.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐