ArrayList


ArrayList is available since Java 1.2

It Implements Following Interfaces:
List
Serializable 
RandomAccess
Cloneable

It Extends the AbstractList class.

The background data structure used for making ArrayList is Grow-able Array or Resizable Array.

ArrayList  has three constructors:

ArrayList()
ArrayList(int initialCapacity)
ArrayList(Collection c)

Characteristics:
  • ArrayList is a resizable.  
  • It grows it size automatically once it is full.
  • It allows duplicate elements.
  • It allows null element.
  • It allows access of particular element using index value.
  • ArrayList non-synchronized
  • ArrayList has its initial capacity.
  • Size of ArrayList is the number elements available in the arraylist.
  • Capacity of the ArrayList grows automatically once its size reach to the capacity.
  • ArrayList is best choice when retrieval operation is your frequent operation.
  • Insertion and deletion operation are slow in arraylist because it need to shift all the other elements when there is a insertion and deletion operation perform on ArrayList.
Example:

import java.util.*;
class ArrayListFlight{
     public static void main(String[] args) {

          ArrayList<String>  arrayList =  new ArrayList<String>();
          arrayList.add("A");
          arrayList.add("B");
          arrayList.add("C"); 
          arrayList.add("A");  
          // It allows duplicate elements                  

          arrayList.add(0, "Z"); 
          // it will add Z at the oth position

          arrayList.set(2, "X");   

   // it will replace the 2 index element with value X

          arrayList.add(null);    
          // It allows null element

         System.out.println(arrayList);          
     }
}

Output:
[Z,A,X,C,A,null]