Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList: how does the size increase?

I have a basic question on Java ArrayList.

When ArrayList is declared and initialized using the default constructor, memory space for 10 elements is created. Now, when I add an 11th element, what happens? Will new memory space be created with 20 (or more) element capacity (this requires copying elements from 1st memory location to new location) OR some thing else?

I checked here. But I didn't find an answer.

Please share the knowledge. Thanks.

like image 408
crazyTechie Avatar asked Dec 15 '10 13:12

crazyTechie


People also ask

How does ArrayList size work?

Since ArrayList is a growable array, it automatically resizes when the size (number of elements in the array list) grows beyond a threshold. Also, when an ArrayList is first created it is called empty ArrayList, and size() will return zero. If you add elements then size grows one by one.

Is ArrayList dynamic in size?

ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Array lists are created with an initial size.

Can ArrayList grow and shrink?

Java ArrayList s do not shrink (even though, of course they do grow) automatically.

Can a method change the size of an ArrayList?

The size of an ArrayList cannot be changed after the ArrayList is initialized. Immediately looking at the question, I would think the answer would be false. If you initialize an ArrayList, you can continue adding unlimited elements to it and the ArrayList will automatically resize.


2 Answers

A new array is created and the contents of the old one are copied over. That's all you know at the API level. Quoting from the docs (my emphasis):

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

In terms of how it actually happens with a specific implementation of ArrayList (such as Sun's), in their case you can see the gory details in the source. But of course, relying on the details of a specific implementation isn't usually a good idea...

like image 78
T.J. Crowder Avatar answered Oct 10 '22 09:10

T.J. Crowder


Sun's JDK6:

I believe that it grows to 15 elements. Not coding it out, but looking at the grow() code in the jdk.

int newCapacity then = 10 + (10 >> 1) = 15.

/**  * Increases the capacity to ensure that it can hold at least the  * number of elements specified by the minimum capacity argument.  *  * @param minCapacity the desired minimum capacity  */ private void grow(int minCapacity) {     // overflow-conscious code     int oldCapacity = elementData.length;     int newCapacity = oldCapacity + (oldCapacity >> 1);     if (newCapacity - minCapacity < 0)         newCapacity = minCapacity;     if (newCapacity - MAX_ARRAY_SIZE > 0)         newCapacity = hugeCapacity(minCapacity);     // minCapacity is usually close to size, so this is a win:     elementData = Arrays.copyOf(elementData, newCapacity); } 

From the Javadoc, it says this is from Java 2 and on, so its a safe bet in the Sun JDK.

EDIT : for those who didn't get what's the connection between multiplying factor 1.5 and int newCapacity = oldCapacity + (oldCapacity >> 1);

>> is right shift operator which reduces a number to its half. Thus,
int newCapacity = oldCapacity + (oldCapacity >> 1);
=> int newCapacity = oldCapacity + 0.5*oldCapacity;
=> int newCapacity = 1.5*oldCapacity ;

like image 41
mgmiller Avatar answered Oct 10 '22 10:10

mgmiller