Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList capacity increment equation

Tags:

java

arraylist

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

However in many books is said that the capacity is doubled... so the books aren't updated or I don't understand well?

like image 761
xdevel2000 Avatar asked Dec 12 '22 19:12

xdevel2000


1 Answers

You're understanding is correct, 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 doesn't fit your requirements, you can implement your own.

like image 75
Jon Freedman Avatar answered Dec 28 '22 22:12

Jon Freedman