ArrayList in Java

  • The ArrayList class in Java extends AbstractList and implements List Interface and also allows null.
  • The ArrayList class is a dynamic array that can grow and shrink at runtime.
  • ArrayList class is expandable or re – sizable array and used behalf of array because array is fixed size and cannot modify once we create it.
  • ArrayList allows duplicate elements.
  • ArrayList support both Iterator and ListIterator for iteration purpose.
  • ArrayList is not synchronized and hence it not thread safe.
  • ArrayList support single dimensional and heterogeneous data.

The following table demonstrates the constructors of the ArrayList class and its methods.

Constructor
Meaning
ArrayList
Constructs an Empty ArrayList Object.
ArrayList(Collection c)
Constructs a list containing the elements of the given collection.
ArrayList(int capacity)
Constructs an ArrayList object with the given initial capacity.

The following program shows a simple use of ArrayList. An array list is created, and then objects of type String are added to it. The list is then displayed. 

Example:

/**
 From SimpleArrayList.java
*/

import java.util.ArrayList;
/**
 *
 * @author JavaHotSpot
 */

public class SimpleArrayList
{
  public static void main(String args[])
  {
      ArrayList alist = new ArrayList();
      alist.add("Book1");
      alist.add("Book2");
      alist.add("Book3");
      alist.add("Book4");
      alist.add("Book5");
      alist.add("Book6");
      alist.add("Book7");
      System.out.println("The size of the ArrayList="+alist.size());
      System.out.println("The elements of ArrayList are..."+"\n"+alist);
      alist.remove("Book7");
      System.out.println("The size of the ArrayList after removing single item="+alist.size());
      System.out.println("The elements of ArrayList are..."+"\n"+alist);
  }
}

Output:
The size of the ArrayList=7
The elements of ArrayList are...
[Book1, Book2, Book3, Book4, Book5, Book6, Book7]
The size of the ArrayList after removing single item=6
The elements of ArrayList are...
[Book1, Book2, Book3, Book4, Book5, Book6]


The following program shows example of ArrayList with using Iterator Interface.

Example:

/**
 From ArrayListwithIterator.java
*/

import java.util.ArrayList;
import java.util.Iterator;
/**
 *
 * @author JavaHotSpot
 */
public class ArrayListwithIterator
{
   public static void main(String args[])
  {
      String ss;
      ArrayList<String> alist = new ArrayList<String>();
      alist.add("Book1");
      alist.add("Book2");
      alist.add("Book3");
      alist.add("Book4");
      alist.add("Book5");
      alist.add("Book6");
      alist.add("Book7");
      System.out.println("Using Iterator Interface"+"\n"+"The elements of ArrayList are...");
      Iterator it = alist.iterator();
      while(it.hasNext())
      {
          ss = (String)it.next();
          System.out.println(ss);
      }
  }
}

Output:
Using Iterator Interface
The elements of ArrayList are...
Book1
Book2
Book3
Book4
Book5
Book6
Book7


The following program shows example of ArrayList with using ListIterator Interface.


Example:

/**
 From ArrayListwithListIterator.java
*/

import java.util.ArrayList;
import java.util.ListIterator;
/**
 *
 * @author JavaHotSpot
 */

public class ArrayListwithListIterator
{
  public static void main(String args[])
  {
      String ss;
      ArrayList<String> alist = new ArrayList<String>();
      alist.add("Book1");
      alist.add("Book2");
      alist.add("Book3");
      alist.add("Book4");
      alist.add("Book5");
      alist.add("Book6");
      alist.add("Book7");
      ListIterator it = alist.listIterator();
      System.out.println("Using ListIterator Interface");
      System.out.println("The elements of ArrayList are(Ascending Order)...");
      while(it.hasNext())
      {
          ss = (String)it.next();
          System.out.println(ss);
      }
      System.out.println("The elements of ArrayList are(Descending Order)...");
      while(it.hasPrevious())
      {
          ss = (String)it.previous();
          System.out.println(ss);
      }
  }
}


Output:
Using ListIterator Interface
The elements of ArrayList are(Ascending Order)...
Book1
Book2
Book3
Book4
Book5
Book6
Book7
The elements of ArrayList are(Descending Order)...
Book7
Book6
Book5
Book4
Book3
Book2