给定示例代码:
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
- ifinitialCapacity
is less than 1
事实上,Java 中的大多数集合都有构造函数,这些构造函数采用指定初始容量的 int 参数。
关于java - Java 中的 PriorityQueue 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13555439/