/*
 * =============================================================================
 * Copyright (c) 1998-2005 Jeffrey M. Hunter. All rights reserved.
 * 
 * All source code and material located at the Internet address of
 * http://www.idevelopment.info is the copyright of Jeffrey M. Hunter, 2005 and
 * is protected under copyright laws of the United States. This source code may
 * not be hosted on any other site without my express, prior, written
 * permission. Application to host any of the material elsewhere can be made by
 * contacting me at jhunter@idevelopment.info.
 *
 * I have made every effort and taken great care in making sure that the source
 * code and other content included on my web site is technically accurate, but I
 * disclaim any and all responsibility for any loss, damage or destruction of
 * data or any other property which may arise from relying on it. I will in no
 * case be liable for any monetary damages arising from such loss, damage or
 * destruction.
 * 
 * As with any code, ensure to test this code in a development environment 
 * before attempting to run it in production.
 * =============================================================================
 */

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;

/**
 * -----------------------------------------------------------------------------
 * The following class provides an example of storing and retrieving objects 
 * from an ArrayList.
 * 
 * A List corresponds to an "ordered" group of elements where duplicates are
 * allowed.
 * 
 * An ArrayList is a good choice for simple sequences.
 * It is an Array based implementation where elements of the List can be
 * accessed directly through get() and set() methods.
 * 
 * ArrayList's give great performance on get() and set() methods, but do not
 * perform well on add() and remove() methods when compared to a LinkedList.
 * 
 * @version 1.0
 * @author  Jeffrey M. Hunter (jhunter@idevelopment.info)
 * @author  http://www.idevelopment.info
 * -----------------------------------------------------------------------------
 */

public class ArrayListExample {

    /* Provides an example of how to work with the ArrayList container */

    public void doArrayListExample() {

        final int MAX = 10;
        int counter = 0;

        System.out.println("----------------------------------------------");
        System.out.println("Create/Store objects in an ArrayList container");
        System.out.println("----------------------------------------------");
        System.out.println();

        List listA = new ArrayList();
        List listB = new ArrayList();

        for (int i = 0; i < MAX; i++) {
            System.out.println("  - Storing Integer(" + i + ")");
            listA.add( new Integer(i) );
        }

        System.out.println("  - Storing String(Alex)");
        listA.add("Alex");

        System.out.println("  - Storing String(Melody)");
        listA.add("Melody");

        System.out.println("  - Storing String(Jeff)");
        listA.add("Jeff");

        System.out.println("  - Storing String(Alex)");
        listA.add("Alex");

        System.out.println("---------------- ");
        System.out.println("Retrieve Objects ");
        System.out.println("---------------- ");
        System.out.println();

        Iterator i = listA.iterator();
        while ( i.hasNext() ) {
            System.out.println( i.next() );
        }

        System.out.println("-------------------------------------");
        System.out.println("Retrieve objects using a ListIterator");
        System.out.println("-------------------------------------");
        System.out.println();

        counter = 0;
        ListIterator li = listA.listIterator();
        while ( li.hasNext() == true ) {
            System.out.println("Element [" + counter + "] = " + li.next());
            System.out.println("  - hasPrevious    = " + li.hasPrevious());
            System.out.println("  - hasNext        = " + li.hasNext());
            System.out.println("  - previousIndex  = " + li.previousIndex());
            System.out.println("  - nextIndex      = " + li.nextIndex());
            System.out.println();
            counter++;
        }

        System.out.println();
        System.out.println("-----------------------------");
        System.out.println("Retrieve objects via an index");
        System.out.println("-----------------------------");
        System.out.println();

        for (int j=0; j < listA.size(); j++) {
            System.out.println("[" + j + "] - " + listA.get(j));
        }

        System.out.println();
        System.out.println("------------------------------------------");
        System.out.println("Search for an object and retrieve an index");
        System.out.println("------------------------------------------");
        System.out.println();

        int locationIndex = listA.indexOf("Jeff");
        System.out.println("Index location of the String \"Jeff\" is: " + locationIndex);  
    }

    /**
     *  ==============================================
     *  Sole entry point to the class and application.
     *  @param args Array of String arguments.
     *  ==============================================
     */

    public static void main(String[] args) {

        ArrayListExample listExample = new ArrayListExample();
        listExample.doArrayListExample();

    }
}
