Core Java Tutorial

Core Java Tutorial

ArrayList Example Program in Java

ArrayList :

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized).
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
Example
package com.candidjava.core;

import java.util.ArrayList;

public class ArrayListExample {

 public static void main(String[] args) {

  ArrayList < String > al = new ArrayList < String > ();

  al.add("hai");

  al.add("123");

  al.add("mathan");

  al.add("lal");

  al.add("mathan");

  al.add("lal");

  al.add("ramya");

  al.add("suji");

  al.add("ravathi");

  al.add("sri");


  System.out.println("Array List .. " + al);

  System.out.println("size ... " + al.size());


 }


}

 

Output:
Array List ..  [hai, 123, mathan, lal, mathan, lal, ramya, suji, ravathi, sri]
size ... 10