Source of Algorithms1.java


  1: // Fig. 19.13: Algorithms1.java
  2: // Using algorithms reverse, fill, copy, min and max.
  3: import java.util.List;
  4: import java.util.Arrays;
  5: import java.util.Collections;
  6: 
  7: public class Algorithms1 
  8: {
  9:    private Character[] letters = { 'P', 'C', 'M' };
 10:    private Character[] lettersCopy;
 11:    private List< Character > list;
 12:    private List< Character > copyList;
 13: 
 14:    // create a List and manipulate it with methods from Collections
 15:    public Algorithms1()
 16:    {
 17:       list = Arrays.asList( letters ); // get List
 18:       lettersCopy = new Character[ 3 ]; 
 19:       copyList = Arrays.asList( lettersCopy ); // list view of lettersCopy
 20: 
 21:       System.out.println( "Initial list: " );
 22:       output( list );
 23: 
 24:       Collections.reverse( list ); // reverse order
 25:       System.out.println( "\nAfter calling reverse: " );
 26:       output( list );
 27: 
 28:       Collections.copy( copyList, list ); // copy List
 29:       System.out.println( "\nAfter copying: " );
 30:       output( copyList );
 31: 
 32:       Collections.fill( list, 'R' ); // fill list with Rs
 33:       System.out.println( "\nAfter calling fill: " );
 34:       output( list );
 35:    } // end Algorithms1 constructor
 36: 
 37:    // output List information
 38:    private void output( List< Character > listRef )
 39:    {
 40:       System.out.print( "The list is: " );
 41: 
 42:       for ( Character element : listRef )
 43:          System.out.printf( "%s ", element );
 44: 
 45:       System.out.printf( "\nMax: %s", Collections.max( listRef ) );
 46:       System.out.printf( "  Min: %s\n", Collections.min( listRef ) );
 47:    } // end method output
 48: 
 49:    public static void main( String args[] )
 50:    {
 51:       new Algorithms1();
 52:    } // end main
 53: } // end class Algorithms1
 54: 
 55: /**************************************************************************
 56:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 57:  * Pearson Education, Inc. All Rights Reserved.                           *
 58:  *                                                                        *
 59:  * DISCLAIMER: The authors and publisher of this book have used their     *
 60:  * best efforts in preparing the book. These efforts include the          *
 61:  * development, research, and testing of the theories and programs        *
 62:  * to determine their effectiveness. The authors and publisher make       *
 63:  * no warranty of any kind, expressed or implied, with regard to these    *
 64:  * programs or to the documentation contained in these books. The authors *
 65:  * and publisher shall not be liable in any event for incidental or       *
 66:  * consequential damages in connection with, or arising out of, the       *
 67:  * furnishing, performance, or use of these programs.                     *
 68:  *************************************************************************/