I am wondering why the default size of a PriorityQueue in Java is 11
. I looked up the implementation and it made things more confusingly for me.
The priority queue is implemented as a heap. Its capacity is adjusted using this function:
/**
* Increases the capacity of the array.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
int newCapacity = ((oldCapacity < 64)?
((oldCapacity + 1) * 2):
((oldCapacity / 2) * 3));
if (newCapacity < 0) // overflow
newCapacity = Integer.MAX_VALUE;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
queue = Arrays.copyOf(queue, newCapacity);
}
I don't understand the initial value 11
for the capacity. I think that the capacity should always be 2
to the number of levels. Any clarification?