import java.util.*; //We are importing the ArrayList class from the util package!

public class ArrayListTest
{
	//The data member, which is our ArrayList
	private ArrayList arrLst;

	//This is the first time we are using the Constructor
	//to actually do something. In this case,
	//We are using it to create the ArrayList object.
	//It is very common to use the constructor to initialize
	//our Object's Data Members.
	public ArrayListTest()
	{
		arrLst=new ArrayList();
	}


	public void addWord(String s)
	{
		//Note: The class "Object" is the universal
		//object. It is the parent of ALL other Objects
		//in Java. Therefore due to Polymorphism,
		//We can use it as a reference to ANY object.
		//Normally it is only used in cases where you
		//need to reference multiple types of objects
		//that do not share any other common parent class
		//or interface. The ArrayList class
		//uses Object for the Add and Get methods
		//so that you can store any type of object in the collection.
		//Here's the add methods signature right from the Java Doc (JDK 1.4.2):
		//public boolean add(Object o)
		//The add method "appends" to the end of the list...
		arrLst.add(s);
	}


	public void printAll()
	{
		String s;

		//We are going to use a for loop
		//to loop from element 0 to the SIZE of the arraylist.
		//The method "size()" returns an int representing the
		//number of elements added to and are current contained
		//within the arraylist.
		for (int i=0; i<arrLst.size(); i++)
		{
			//The "get" method will return
			//the Object to us. We need to type case
			//the Object back to the String object.
			s=(String)arrLst.get(i);

			//Print out the String on it's own line.
			System.out.println(s);
		}
	}


	//This method will remove
	//an element from the ArrayList
	//at the given index.
	public void removeWordAt(int index)
	{
		//We should only remove from the array list
		//if the index is a valid index within the Array List.
		//This is why we are checking if the value of index
		//is between 0 and the size of the array list.
		if (index>=0 && index<arrLst.size())
		{
			//If the index is valid
			//Do the actual removal.
			arrLst.remove(index);
		}
	}


	//Just a wrapper method around
	//the "clear()" method of array list
	//which will REMOVE all elements from the list.
	public void clearAllWords()
	{
		arrLst.clear();
	}


	public static void main(String[] args)
	{
		//Create a new ArrayListTest object to use
		ArrayListTest alTest=new ArrayListTest();

		//Add Strings to my array list using the addWord
		//method on the ArrayListTest class we just created...
		alTest.addWord("Hello");
		alTest.addWord("World");
		alTest.addWord("This");
		alTest.addWord("Is");
		alTest.addWord("My");
		alTest.addWord("First");
		alTest.addWord("Test");
		alTest.addWord("With");
		alTest.addWord("ArrayLists!");

		//Use the print all method on the ArrayListTest
		//class we created to print each word we added
		//on it's own line on the screen.
		alTest.printAll();


		//Print a blank like to separate output!
		System.out.println();


		//Let's test the remove method and remove word "First"
		//which if we count is the 6th element which is array index 5.
		//We need to use array index 5 instead of the actual element number
		//index the first element in the array list is called element 0 not 1!
		//Basically as we mentioned many times, ArrayList is basically a
		//growable array!
		alTest.removeWordAt(5);

		//Let's print again, to show the changes to the list!
		alTest.printAll();


		//Since we are done with the list.
		//It's good practice to clear the list.
		//This helps Java clear the memory of unused data FASTER.
		alTest.clearAllWords();
	}
}
