Java ArrayList

 

ArrayList

 

public class ArrayList<E>
    extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, Serializable
 

 

Below is an ArrayList Example showing how collections are manipulated using an ArrayList

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;

public class ArrayListExample {


    public static void main(String[] args) {

//        ArrayList Creation
        List arraylistA = new ArrayList();
        List arraylistB = new ArrayList();

//        Adding elements to the ArrayList
        for (int i = 0; i < 5; i++) {
            arraylistA.add(new Integer(i));
        }

        arraylistB.add("beginner");
        arraylistB.add("java");
        arraylistB.add("tutorial");
        arraylistB.add(".");
        arraylistB.add("com");
        arraylistB.add("java");
        arraylistB.add("site");
        
//      Iterating through the ArrayList to display the Contents. 
        Iterator i1 = arraylistA.iterator();
        System.out.print("ArrayList arraylistA --> ");
        while (i1.hasNext()) {
            System.out.print(i1.next()+ " , ");
        }
        System.out.println();
        System.out.print("ArrayList arraylistA --> ");
        for (int j=0; j < arraylistA.size(); j++) {
            System.out.print(arraylistA.get(j)+ " , ");
        }
        System.out.println();
        Iterator i2 = arraylistB.iterator();
        System.out.println("ArrayList arraylistB --> ");
        while (i2.hasNext()) {
            System.out.print(i2.next()+ " , ");
        }
        
        System.out.println();
        System.out.println();
        System.out.println("Using ListIterator to retrieve ArrayList Elements");
        System.out.println();

        ListIterator li1 = arraylistA.listIterator();
//       next(), hasPrevious(), hasNext(), hasNext() nextIndex() can be used with a 
//        ListIterator interface implementation
        System.out.println("ArrayList arraylistA --> ");
        while (li1.hasNext()) {
        	System.out.print(li1.next()+ " , ");
        }

        System.out.println();
        
//        Searching for an element in the ArrayList
        int index = arraylistB.indexOf("java");
        System.out.println("'java' was found at : " + index); 
        int lastIndex = arraylistB.lastIndexOf("java");
        System.out.println("'java' was found at : " + lastIndex+" from the last");
        System.out.println();
        
//        Getting the subList from the original List
        List subList = arraylistA.subList(3, arraylistA.size());
        System.out.println("New Sub-List(arraylistA) from index 3 to " 
        				+ arraylistA.size() + ": " + subList);
        System.out.println();
        
//        Sort an ArrayList
        System.out.print("Sorted ArrayList arraylistA --> ");
        Collections.sort(arraylistA);
        System.out.print(arraylistA);
        System.out.println();

//      Reversing an ArrayList
        System.out.print("Reversed ArrayList arraylistA --> ");
        Collections.reverse(arraylistA);
        System.out.println(arraylistA);
        System.out.println();
        
//		Checking emptyness of an ArrayList
        System.out.println("Is arraylistA empty?   " + arraylistA.isEmpty());
        System.out.println();

//        Checking for Equality of ArrayLists
        ArrayList arraylistC = new ArrayList(arraylistA);
        System.out.println("arraylistA.equals(arraylistC)? " 
						+ arraylistA.equals(arraylistC));
        System.out.println();

//      Shuffling the elements of an ArrayList in Random Order
        Collections.shuffle(arraylistA, new Random());
        System.out.print("ArrayList arraylistA after shuffling its elements--> ");
        i1 = arraylistA.iterator();
        while (i1.hasNext()) {
            System.out.print(i1.next()+ " , ");
        }
        System.out.println();
        System.out.println();
//        Converting an ArrayList to an Array
        Object[] array = arraylistA.toArray();
        for (int i=0; i < array.length; i++) {
            System.out.println("Array Element [" + i + "] = " + array[i]);
        }
        System.out.println();

//        Clearing ArrayList Elements
        arraylistA.clear();
        System.out.println("arraylistA after clearing  : " + arraylistA);
        System.out.println();
    }

}


     

Output

ArrayList arraylistA --> 0 , 1 , 2 , 3 , 4 ,
ArrayList arraylistA --> 0 , 1 , 2 , 3 , 4 ,
ArrayList arraylistB -->
beginner , java , tutorial , . , com , java , site ,

Using ListIterator to retrieve ArrayList Elements

ArrayList arraylistA -->
0 , 1 , 2 , 3 , 4 ,
'java' was found at : 1
'java' was found at : 5 from the last

New Sub-List(arraylistA) from index 3 to 5: [3, 4]

Sorted ArrayList arraylistA --> [0, 1, 2, 3, 4]
Reversed ArrayList arraylistA --> [4, 3, 2, 1, 0]

Is arraylistA empty? false

arraylistA.equals(arraylistC)? true

ArrayList arraylistA after shuffling its elements--> 3 , 2 , 1 , 0 , 4 ,

Array Element [0] = 3
Array Element [1] = 2
Array Element [2] = 1
Array Element [3] = 0
Array Element [4] = 4

arraylistA after clearing : []
 

Download ArrayListExample.java