<分区>
我有一个简单的问题。我正在我的项目中实现 PriorityQueue
。我的问题是我可以为 PriorityQueue
is Java 设置固定大小吗?
<分区>
我有一个简单的问题。我正在我的项目中实现 PriorityQueue
。我的问题是我可以为 PriorityQueue
is Java 设置固定大小吗?
最佳答案
正如此处的 oracle 文档所述, http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html :
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.
一个可能的解决方法是您可以在执行任何操作之前检查大小:
if (q.size() <= QUEUE_LIMIT)
//your code
关于java - 如何在 Java 中为 PriorityQueue 设置固定大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988623/