ArrayList in Java

ArrayList in Java is an implementation of the List interface. Few points about ArrayList
1. Internally uses array but can be dynamically resized.
2. Allows duplicate elements
3. Maintains the insertion order.
4. Is not synchronized.
ArrayList in Java

ArrayList in Java

In this article we will see the basics of ArrayList in java. Some of the topics covered are
1. How ArrayList works internally
2. Methods of ArrayList
3. When to use an ArrayList.

How ArrayList works internally

ArrayList internally uses an array of Object to store the elements.

You will notice that the Object[] is marked as transient, hence serialization doesn’t happen automatically. ArrayList has its own methods private void readObject(java.io.ObjectInputStream s) and private void writeObject(java.io.ObjectOutputStream s)

ArrayList can be created using either of the 2 constructors provided

The default constructor creates an ArrayList with the capacity of 10, but you can customize the capacity using the other constructor and provide the capacity you need.

As earlier stated, ArrayList is an re-sizable List, so lets see how ArrayList dynamically grows when elements are added.
When a new element is added, the add method in ArrayList first checks the capacity. If ArrayList has space it will add the new element, else it will call the grow() method for increasing the capacity.
From java 7, the capacity is increased using a shift operator. A new array is created with new capacity and old data is copied into the new array.

Constructors

ArrayList()
Constructs an empty list with an initial capacity of ten.

ArrayList(Collection c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.

ArrayList methods

Below are few of the methods that are mostly used for ArrayList manupilation

1. public boolean add(E e)
Appends the specified element to the end of this list.

2. public void add(int index, E element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

3. public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

4. public boolean remove(Object o)
Removes the first occurrence of the specified element from this list,if it is present.

5. public void ensureCapacity(int minCapacity)

6. public List subList(int fromIndex, int toIndex)

7. void clear()
It is used to remove all of the elements from this list.

8. boolean removeAll(Collection c)
It is used to remove all the elements from the list.

When to use ArrayList

For deciding when to use an ArrayList, lets see the internal implementations of add and remove methods.

If you see the add(int index, E element) you will see that once the element is added at the specified location, the other elements are shifted to the right. This creates an overhead while adding elements to a specific position in ArrayList. Similarly for remove(int index), once the element is removed, the other elements after that location are shifted to left. So clearly using ArrayList for adding/removing elements from a specific position is not advisable. But if you see get(int index) directly gives you to the element from the array (elementData[]). Hence using ArrayList is effective if you have frequent search operation.

This was some basics on ArrayList. You can also go through few examples on
1. How to Iterate ArrayList in java
2. Sorting of ArrayList in Java using Comparators

Reference:
1. ArrayList Java Docs

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn