ArrayList Power Increase Equation

In JDK 1.7 in ArrayList.java, the ensureCapacity method increases the capacity of the array using the following expression: int newCapacity = oldCapacity + (oldCapacity >> 1) , so it seems that the new capacity will be almost 50% more than the old.

However, many books say capacity doubles ... so books are not being updated or am I poorly understanding?

+4
source share
3 answers

You get it right, newCapacity is 50% more than oldCapacity

In Java 6, newCapacity is calculated as

 int newCapacity = (oldCapacity * 3)/2 + 1; 

This is the beauty of an open source language such as Java, you can see the implementation - if it does not meet your requirements, you can implement your own.

+5
source

From ArrayList javadoc :

The details of the growth policy are not indicated because the addition of an element has a constant amortized time value.

In other words, books may be accurate for other implementations, but nothing is guaranteed - and the Java 7 source is still document compatible, but shows that books are too specific.

+5
source

ArrayLists in Java increases capacity by 50%. However, a Java vector class that works similar to ArrayLists but offers synchronization doubles capacity. This is probably due to the confusion in your books.

0
source

All Articles