// ArrayListTest.java
// Using the Collection interface.
package chapter36;

import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class ArrayListTest
{
    private static final String[] colors =
            { "MAGENTA", "RED", "WHITE", "BLUE", "CYAN" };
    private static final String[] removeColors =
            { "RED", "WHITE", "BLUE" };

    // create ArrayList, add Colors to it and manipulate it
    public ArrayListTest()
    {
        // Create ArrayList objects and assign their references
        // to variable list and removeList, respectively
        List list = new ArrayList();
        List removeList = new ArrayList();

        // Use List method add to add objects to list and removeList, respectively
        for ( String color : colors )
            list.add( color );

        // add elements in removeColors to removeList
        for ( String color : removeColors )
            removeList.add( color );

        System.out.println( "ArrayList: " );

        // output list contents
        // Use List method size to get the number of ArrayList elements
        // Use List method get to retrieve individual element values
        for ( int count = 0; count < list.size(); count++ )
            System.out.printf( "%s ", list.get( count ) );

        // remove colors contained in removeList
        // Method removeColors takes two Collections as arguments
        removeColors( list, removeList );

        System.out.println( "\n\nArrayList after calling removeColors: " );

        // output list contents
        for ( Object color : list )
           System.out.printf( "%s ", color );
        
    } // end ArrayListTest constructor

    // remove colors specified in collection2 from collection1
    // Method removeColors allows any Collections containing strings
    // to be passed as arguments to this method
    private void removeColors( Collection collection1, Collection collection2 )
    {
        // get iterator
        Iterator iterator = collection1.iterator();

        // loop while collection has items
       // Iterator method hasNext determines whether the Iterator contains more elements
        while ( iterator.hasNext() )
            // Collection method contains determines whether collection2 contains the element returned by next
            if ( collection2.contains( iterator.next() ) )
                // Use Iterator method remove to remove String from Iterator
                iterator.remove(); // remove current Color
    } // end method removeColors

    public static void main( String args[] )
    {
        new ArrayListTest();
    } // end main
} // end class CollectionTest
