Today, I'm back with a basic question about ArrayList.
As we know ArrayList is a resizeable array, that means an array list are created with initial size and its size can be increased as needed. But, have you ever wondered about default initial size of an array list, how increasing size works?
1. Default initial size of an arraylist.
And here is the answer:
Now, I'm using
jdk1.7.0_79. As we see at above picture, the
DEFAULT_CAPACITY = 10. When we didn't declare the capacity of ArrayList, the default capacity is
2. How does increasing size work?
First at all, let's consider about below method of class
When you tell JVM that you would like to add one more element to an existing arraylist. And here is what happened.
newCapacity = oldCapacity + (Capacity >>1). That's same with
newCapacity = (oldCapacity*3)/2. Example:
oldCapacity = 10. And then,
newCapacity = 10*3/2 = 15.
newCapacity < minCapacitythen it uses
minCapacityas new capacity of array list.
in another case, if
newCapacityis more than maximum size (that's declared by JVM), an
OutOfMemoryErrorwill be throwed.
finally, a new array list will be created , all data of old arrays will be copied to it, and old array list will be grabage collected.
In another way, we can say , ArrayList increases its size by 50%
Thanks for reading
Have a nice weekend.
All Rights Reserved