/**
 * Demonstrates an ArrayList
 * 
 * @author TK Rogers 
 * @version 12/31/10
 */
import java.util.ArrayList ; // Required for using arrayLists
public class ArrayListDemo {
    public static void main ( ) {
        // Note that ArrayLists cannot hold primative values like int. They can only hold 
        // objects. Interger is a wrapper class that holds int type objects.
        ArrayList <Integer> list1 = new ArrayList <Integer> ( ) ;
        ArrayList                 list2 = new ArrayList ( ) ;

        list1.add ( 7 ) ;

        list2.add ( 7 ) ;
        list2.add ( "cat" ) ; // Note: a string is stored in the same arrayList as an Integer.

        if ( list1.get ( 0 ) == list2.get ( 0 ) ) {
            System.out.println ( "list1.get(0) == list2.get(0) is true\n" ) ;
        }
        
        System.out.println ( "list1.get ( 0 ) / 2 = " + list1.get ( 0 ) / 2.0 ) ;
		 System.out.println ( "( String )list2.get ( 0 ) = " + list2.get ( 0 ) + "\n" ) ;
        // Note the line of code show below will not compile. Why?
        // System.out.println ( "list2.get ( 0 ).intValue ( ) * 2 = " + list2.get ( 0 ) / 2 ) ;

        System.out.println ( "( Integer ) list2.get( 0 ) / 2 = " + ( Integer ) list2.get ( 0 ) / 2 ) ;
        System.out.println ( "( String ) list2.get ( 1 ) = " + ( String ) list2.get ( 1 ) + "\n" ) ;
        String wordArray [  ] = { "cat", "hat", "scat", "chat" } ;
        ArrayList<String> wordList = new ArrayList ( ) ;
        wordList.add ( "cat" ) ;
        wordList.add ( "hat" ) ;
        wordList.add ( "scat" ) ;
        wordList.add ( "chat" ) ;
        System.out.println ( "# of words with 3 letters: " + count ( wordList, 3 ) + "\n" ) ;
        
        System.out.println( "The array version outputs: " + wordArray ) ;
        System.out.println( "The arrayList version outputs: " + wordList + "\n" ) ;
    }
    
    /** Purpose: counts the number of strings in an arrayList
     *  that have a particular length.
     *  Preconditions; an array list of strings and an integer
     *  representing the length of the strings that are counted.
     *  Post conditions: returns an integer representing the
     *  number of words with a given length.
     */

    public static int count ( ArrayList <String> arr, int length ) {
        int count = 0;
        for ( int x = 0; x < arr.size ( ); x++ ) {
            if ( arr.get ( x ).length ( ) == length ) {
                count++;  
            }
        }
        return count ;
    }
}