ArrayList in Java

73
Advertisement

ArrayList is the child class of List Interface, ArrayList implements the resizable or growable array data structure. List Interface is the child of Collection Framework.ArrayList is used to store the Java Objects, it is not comfortable to store the values of primitive data types.

List Interface Structure
List Interface Structure

The above image shows how ArrayList arranged under the collection Framework.

There are some points to always remember about ArrayList:

  • The underlying data structure is a Resizable or growable array, so the ArrayList is not fixed in size.
  • We can store the duplicate elements also in the ArrayList and only differentiate duplicate elements by their index.
  • The order is preserved in the ArrayList means the elements are always in the order in which we have inserted them into ArrayList.
  • ArrayList implements RandomAccessInterface so we can access any element at the same speed, Hence if our frequent operation is to retrieve the elements so the best suitable data structure is ArrayList.
  • The default initial capacity of ArrayList is 10.
  • We can insert null as the value in ArrayList.
  • We need to import java.util.ArrayList package in order to use the ArrayList class.
Java
import java.util.ArrayList;
How to Create ArrayList Object?
Constructor:
ConstructorDescription
ArrayList()Constructs an empty list with an initial capacity of ten.
ArrayList​(int initialCapacity)Constructs an empty list with the specified initial capacity.
ArrayList​(Collection<? extends E> c)Constructs a list containing the elements of the specified collection
in the order, they are returned by the collection’s iterator.

1. ArrayList a = new ArrayList();

Creates an empty ArrayList of the initial capacity of 10. When ArrayList reaches its maximum capacity then a new ArrayList will be created with new capacity:

new_capacity = initial_capacity*3/2+1;

2. ArrayList a = new ArrayList(int initialCapacity);

Creates the ArrayList with the specified initial capacity.

This constructor is very helpful when you have a very large number of values to store in ArrayList and don’t want to add values, again and again, it’s capacity growth as discussed above. As element added in the ArrayList, its capacity grows automatically, In the background, a new array list with the size current_Capacity*3/2+1; is created and all the elements of the previous ArrayList copied to the new ArrayList and the previous ArrayList are then garbage collected.

It gives the IllegalArgumentException when the initial Capacity is negative.

3. ArrayList a = new ArrayList(Collection c);

Creates an equivalent ArrayList using a Collection Object.

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
Parameters: c – the collection whose elements are to be placed into this list.
Throws: NullPointerException – if the specified collection is null.

For practice and better understanding we will create here an ArrayList Object:

Java
ArrayList<String> a = new ArrayList();

The above examples shows that we have created an empty ArrayList with the name aand ArrayList<String>shows that we can only stores the String type values in this ArrayList that prevents the ClassCastException in Runtime. We will discuss ClassCastException in the later post.

Useful methods in ArrayList:

Here we are discussing about some most useful methods while accessing the ArrayList, these methods will help you get the most from the ArrayList.

How to Add elements in ArrayList?

  1. public boolean add (E e)

    If you want to add a single element at the end of the ArrayList, then probably you can use this method to add the element.

    Parameters:  e – element to be appended to this list

Java
// Add element like below shows in example

a.add("One");
a.add("Two");

Output:  [One, Two]

2. public void add(int index,E element)

If the requirement is to add the element in ArrayList in some specific location then you should use the above method to insert an element in any specified location in ArrayList.

Parameters: index – index at which the specified element is to be inserted

element – element to be inserted

Throws: IndexOutOfBoundsException – if the index is out of range (index < 0 || index > size())

Java
// using this method add element shows below 

a.add(1,"Before Two");
a.add(3,"Third");

Output: [One, Before Two, Two, Third]

3.public boolean addAll(Collection<? extends E> c)

If you have an Collection Object and want to add the elements of the collection Object in ArrayList hen you surely have to use this method. This method add all the elements at the end of the ArrayList.

Parameters: c – collection containing elements to be added to this list.

Throws: NullPointerException – if the specified collection is null.

Java
ArrayList<String> b = new ArrayList();
b.add("Sunday");
b.add("Monday");
    
a.addAll(b);

In this example we have created an other ArrayList ArratList<String> b and added some elements and then we have added ArrayList b into ArrayList a.

Output: [One, Before Two, Two, Third, Sunday, Monday]

4. public boolean addAll(int index,Collection<? extends E> c)

If you want to insert Collection object into the ArrayList at some specified position then you should use this method because same as public void add(int index,E element) you can insert the whole Collection object into an ArrayList.

Parameters:

index – index at which to insert the first element from the specified collection.

c – collection containing elements to be added to this list.

Throws:

IndexOutOfBoundsException – if the index is out of range (index < 0 || index > size()).

NullPointerException – if the specified collection is null.

Java
ArrayList<String> c = new ArrayList();
    
c.add("January");
c.add("February");
    
a.addAll(4,c);

In this example we have created a new ArrayList<String> c with some values and then extending the previous ArrayList a with the elements of ArrayList c.

Some of the most useful methods are described into the table, use them into your programming and practice:

MethodsDescription
public int size()Returns the number of elements in this list.
public boolean isEmpty()Returns true if this list contains no elements.
public boolean contains(Object o)Returns true if this list contains the specified element. More formally.
public int indexOf(Object o)Returns the index of the first occurrence of the specified element in this list,
or -1 if this list does not contain the element.
public E get(int index)Returns the element at the specified position in this list.
public E remove(int index)Removes the element at the specified position in this list.
public void clear()Removes all of the elements from this list.

You may also learn these to build the Knowledge: