java - 如何在 Java 中为 PriorityQueue 设置固定大小?

标签 java priority-queue

<分区>

我有一个简单的问题。我正在我的项目中实现 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/

相关文章:

java - 生成 Web 应用程序中所有信息的报告

java - 比较器和优先级队列

haskell - 如何实现一个最优的、纯功能性的、双端优先级队列?

java - 以编程方式退出应用程序的适当方法是什么?

java - 无法读取 linux 操作系统中 Apache Web 服务器中的 Jquery、css

java - 如何找到正确的用户名和密码标签?

java - JodConverter 处理转换文档的多个请求

c++ - 使用 `std::greater` 通过 `priority_queue` 创建最小堆的原因

algorithm - 什么时候使用优先队列?

javascript - 异步优先级队列 - 数字越大优先级越高吗?