Arraylist in Java

Posted on Updated on

Before we proceed with understanding of ArrayList, let us understand what brought us to the creation of ArrayList.
While we are working with data, we certainly use array in our program. Array has certain disadvantages such as size limitation, insertion or removal of element in middle indexes and few more. All these disadvantages has been resolved in ArrayList.

ArrayList class extends AbstractList and implements List interface. ArrayList size grows dynamically and it grows as needed. An ArrayList is a variable length array of object references. That is, an ArrayList can dynamically increase or decrease its size.
ArrayList has constructors as follows:

  1. ArrayList() : This constructor builds an empty array list.
  2. ArrayList(Collection c) : This constructor builds an array list with elements in collection c.
  3. ArrayList(int capacity) : This constructor builds an array with initial size capacity. This will ensure that ArrayList has initial size of capacity.

Let’s see an example to understand better.


Initial size of arraylst is 0

Size of arraylst after addition of element is 4

Element of arraylst are [1, 2, 3, 4]

Size of arraylst after removal of element is 3

Element of arraylst are [1, 2, 4]


As we can clearly see, how the size of ArrayList can increase and decrease dynamically.

Although size of ArrayList dynamically grows but we can ensure its minimum size by manually calling a method ensureCapacity(). This method is to prevent the several reallocation of array size. The repetitive adjustment in size of ArrayList has impact on performance and costly in terms of time.

Signature of ensureCapacity() is as:

void ensureCapacity(int cap) – Here cap is the new capacity.

Vice-versa we can also reduce the size of ArrayList by calling trimToSize(). Signature is as:

void trimToSize()

ArrayList having advantages over Array, also has certain disadvantages.

We have discussed above advantages of ArrayList and let’s see some of its disadvantages-

  1. ArrayList internally uses Array, if its size is altered then basically the operation performed is creation of new array with New size and then copying all the values to new array. This is overall time consuming and has impact on performance.
  2. ArrayList cannot store primitives, it can only contain object, while Array can store both. The JAVA 5 has introduced Autoboxing concept in which we can store primitive type elements. But the fact is it only gives an impression of adding primitives, it actually converts primitives to object.

Why do should I convert ArrayList to Array?

I am pretty sure that you’re puzzling as we studied ArrayList as an advantage over Array and yet again we are converting ArrayList to Array. There are several reason as to convert ArrayList to Array.

  1. To obtain faster processing times for certain operations.
  2. To pass an array to method that is not overloaded to accept a collection
  3. To integrate your newer, collection based code with legacy code that doesn’t understand collections.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.