Source of BinarySearchTest.java


  1: // Fig. 19.14: BinarySearchTest.java
  2: // Using algorithm binarySearch.
  3: import java.util.List;
  4: import java.util.Arrays;
  5: import java.util.Collections;
  6: import java.util.ArrayList;
  7: 
  8: public class BinarySearchTest 
  9: {
 10:    private static final String colors[] = { "red", "white",  
 11:       "blue", "black", "yellow", "purple", "tan", "pink" };
 12:    private List< String > list; // ArrayList reference
 13: 
 14:    // create, sort and output list 
 15:    public BinarySearchTest()
 16:    {
 17:       list = new ArrayList< String >( Arrays.asList( colors ) );  
 18:       Collections.sort( list ); // sort the ArrayList
 19:       System.out.printf( "Sorted ArrayList: %s\n", list );
 20:    } // end BinarySearchTest constructor
 21: 
 22:    // search list for various values
 23:    private void search()
 24:    {
 25:       printSearchResults( colors[ 3 ] ); // first item
 26:       printSearchResults( colors[ 0 ] ); // middle item
 27:       printSearchResults( colors[ 7 ] ); // last item
 28:       printSearchResults( "aqua" ); // below lowest
 29:       printSearchResults( "gray" ); // does not exist
 30:       printSearchResults( "teal" ); // does not exist
 31:    } // end method search
 32: 
 33:    // helper method to perform searches
 34:    private void printSearchResults( String key )
 35:    {
 36:       int result = 0;
 37: 
 38:       System.out.printf( "\nSearching for: %s\n", key );
 39:       result = Collections.binarySearch( list, key );
 40:       
 41:       if ( result >= 0 )
 42:          System.out.printf( "Found at index %d\n", result );
 43:       else
 44:          System.out.printf( "Not Found (%d)\n",result );
 45:    } // end method printSearchResults
 46: 
 47:    public static void main( String args[] )
 48:    {
 49:       BinarySearchTest binarySearchTest = new BinarySearchTest();
 50:       binarySearchTest.search();
 51:    } // end main     
 52: } // end class BinarySearchTest
 53: 
 54: /**************************************************************************
 55:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 56:  * Pearson Education, Inc. All Rights Reserved.                           *
 57:  *                                                                        *
 58:  * DISCLAIMER: The authors and publisher of this book have used their     *
 59:  * best efforts in preparing the book. These efforts include the          *
 60:  * development, research, and testing of the theories and programs        *
 61:  * to determine their effectiveness. The authors and publisher make       *
 62:  * no warranty of any kind, expressed or implied, with regard to these    *
 63:  * programs or to the documentation contained in these books. The authors *
 64:  * and publisher shall not be liable in any event for incidental or       *
 65:  * consequential damages in connection with, or arising out of, the       *
 66:  * furnishing, performance, or use of these programs.                     *
 67:  *************************************************************************/