Java Array List


Java comes with many Data Structures of which very often used is ArrayList. The advantage of ArrayList is it accepts duplicates and the methods are not synchronized. For this reason, the execution of Java array list is faster. The elements can be added or retrieved very fast.

ArrayList is derived from List interface and again List is derived from Collection interface. Being subclass, ArrayList can make use of all the methods of List interface and Collection interface.

Following is a simple example on Java array list where elements are added and retrieved.
import java.util.*;
public class ArrayListDemo
{
  public static void main(String args[])
  {
    ArrayList al1 = new ArrayList();
    al1.add("Hello");    
    al1.add(10);
    al1.add(10.5);
    al1.add(true);
                                     // printing elements
    System.out.println("\n"+al1);

    System.out.println("\nPrinting array list elements with Iterator interface");
    Iterator it = al1.iterator();
    while(it.hasNext())
    {
      System.out.println(it.next());
    }
  }
}

Java Array ListOutput Screenshot on Java Array List

ArrayList object al1 is added with different types of elements with add() method. The iterator() method returns an object of Iterator interface. Iterator object it includes all the elements of al1. hasNext() method iterates all the elements and next() method prints each element.

To play with ArrayList and to know more operations go through.

1. ArrayList Introduction
2. ArrayList Methods
3. ArrayList Iterators
4. ArrayList Operations
5. ArrayList Special