java - Java 中的 PriorityQueue 构造函数

标签 java queue priority-queue

给定示例代码:

Comparator<Node> comparator = this.createCompartor(algorithmChoice , matrix);

this.m_openList1 = new PriorityQueue<Node>(100, comparator);

PriorityQueue 构造函数中的 100 代表什么?

最佳答案

这是PriorityQueue的初始容量.如果您事先知道要添加多少项目(或有一个近似值),您通常会明确指定初始容量。

这也可以在 documentation 中看到:

public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)

Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

Parameters:
- initialCapacity - the initial capacity for this priority queue.
- comparator - the comparator used to order this priority queue. If null then the order depends on the elements' natural ordering.

Throws:
- IllegalArgumentException - if initialCapacity is less than 1

事实上,Java 中的大多数集合都有构造函数,这些构造函数采用指定初始容量的 int 参数。

关于java - Java 中的 PriorityQueue 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13555439/

相关文章:

java - Java进程内存大小和堆大小的区别

java - 我们可以在 Java 中调用 Windows cmd 命令吗?

laravel - Beanstalkd/Pheanstalk - 根本无法让它工作! (WAMP/PHP/Laravel)

java - 为什么会得到以下结果?

java - 对优先队列中的这一行感到困惑

data-structures - 优先级队列的优先级总是需要整数?

java - 从Hadoop获取用户和组列表?

java - java中 "this.x"是什么意思?

java - 何时清理ReferenceQueue?被系统清理了?

c - malloc 无法为队列中的结构创建空间