//TestArrayList.java
//Shows two ArrayList constructors (default, and one parameter)
//as well as several ways to display the values, and uses of
//the following ArrayList methods:
//size()
//add() //two versions
//remove()
//set()
//contains()
//indexOf()
//clear()
//isEmpty()

import java.util.ArrayList;
import java.util.Scanner;

public class TestArrayList
{
    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args)
    {
        //Default initial capacity will be 10.
        //ArrayList<Integer> a = new ArrayList<Integer>();
        //Initial capacity can be specified ...
        ArrayList<Integer> a = new ArrayList<Integer>(20);

        //Size will be 0 (not the same as capacity).
        System.out.println("Size of new ArrayList = " + a.size());
        System.out.print("1Press Enter to continue ... ");
        keyboard.nextLine();

        //Add five values and check size again.
        for (int i=1; i<=5; i++) a.add(i);
        System.out.println("Size after adding 5 values = " + a.size());
        System.out.print("2Press Enter to continue ... ");
        keyboard.nextLine();

        //Display all values in three different ways ...
        for (int i=0; i<a.size(); i++) System.out.print(a.get(i) + " ");
        System.out.println();
        for (Integer entry: a) System.out.print(entry + " ");
        System.out.println();
        for (int entry: a) System.out.print(entry + " ");
        System.out.println();
        System.out.print("3Press Enter to continue ... ");
        keyboard.nextLine();

        //Insert a new value at index 2 and display again.
        a.add(2, 555);
        for (Integer entry: a) System.out.print(entry + " ");
        System.out.println();
        System.out.print("4Press Enter to continue ... ");
        keyboard.nextLine();

        //Remove value at index 2 and display again.
        a.remove(2);
        for (Integer entry: a) System.out.print(entry + " ");
        System.out.println();
        System.out.print("5Press Enter to continue ... ");
        keyboard.nextLine();

        //Set new value at index 2 and display again.
        a.set(2, 555);
        for (Integer entry: a) System.out.print(entry + " ");
        System.out.println();
        System.out.print("6Press Enter to continue ... ");
        keyboard.nextLine();

        //Check if an integer value and then an arbitrary object
        //(in this case, a string) are in the ArrayList.
        System.out.println(a.contains(4));
        System.out.println(a.contains(6));
        System.out.println(a.contains("Hello, world!"));
        System.out.print("7Press Enter to continue ... ");
        keyboard.nextLine();

        //Find where in the ArrayList an element is located.
        //(in this case, a string) are in the ArrayList.
        System.out.println(a.indexOf(4));
        System.out.println(a.indexOf(6));
        System.out.println(a.indexOf("Hello, world!"));
        System.out.print("8Press Enter to continue ... ");
        keyboard.nextLine();

        //Clear the ArrayList, then check to make sure it's empty.
        //(in this case, a string) are in the ArrayList.
        a.clear();
        System.out.println(a.isEmpty());
        System.out.print("9Press Enter to continue ... ");
        keyboard.nextLine();
    }
}

