// Algorithms1.java
// Using algorithms reverse, fill, copy, min and max.
package chapter36;

import java.util.List;
import java.util.Arrays;
import java.util.Collections;

public class Algorithms1
{
    private Character[] letters = { 'P', 'C', 'M' };
    private Character[] lettersCopy;
    private List< Character > list;
    private List< Character > copyList;

    // create a List and manipulate it with methods from Collections
    public Algorithms1()
    {
        list = Arrays.asList( letters ); // get List
        lettersCopy = new Character[ 3 ];
        copyList = Arrays.asList( lettersCopy ); // list view of lettersCopy

        System.out.println( "Initial list: " );
        output( list );

        // Use method reverse of class Collections to obtain List in reverse order
        Collections.reverse( list ); // reverse order
        System.out.println( "\nAfter calling reverse: " );
        output( list );

        // Use method copy of class Collections to obtain copy of List
        Collections.copy( copyList, list ); // copy List
        System.out.println( "\nAfter copying: " );
        output( copyList );

        // Use method fill of class Collections to populate List with the letter ‘R’
        Collections.fill( list, 'R' ); // fill list with Rs
        System.out.println( "\nAfter calling fill: " );
        output( list );
    } // end Algorithms1 constructor

    // output List information
    private void output( List< Character > listRef )
    {
        System.out.print( "The list is: " );

        for ( Character element : listRef )
            System.out.printf( "%s ", element );

        // Obtain maximum value in List
        System.out.printf( "\nMax: %s", Collections.max( listRef ) );
        // Obtain minimum value in List
        System.out.printf( "  Min: %s\n", Collections.min( listRef ) );
    } // end method output

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