Source of UsingArrays.java


  1: // Fig. 19.2: UsingArrays.java
  2: // Using Java arrays.
  3: import java.util.Arrays;
  4: 
  5: public class UsingArrays 
  6: {
  7:    private int intArray[] = { 1, 2, 3, 4, 5, 6 };
  8:    private double doubleArray[] = { 8.4, 9.3, 0.2, 7.9, 3.4 };
  9:    private int filledIntArray[], intArrayCopy[];
 10: 
 11:    // constructor initializes arrays
 12:    public UsingArrays()
 13:    {
 14:       filledIntArray = new int[ 10 ]; // create int array with 10 elements
 15:       intArrayCopy = new int[ intArray.length ];
 16: 
 17:       Arrays.fill( filledIntArray, 7 ); // fill with 7s
 18:       Arrays.sort( doubleArray ); // sort doubleArray ascending
 19: 
 20:       // copy array intArray into array intArrayCopy
 21:       System.arraycopy( intArray, 0, intArrayCopy,
 22:          0, intArray.length );
 23:    } // end UsingArrays constructor
 24: 
 25:    // output values in each array
 26:    public void printArrays()
 27:    {     
 28:       System.out.print( "doubleArray: " );
 29:       for ( double doubleValue : doubleArray )
 30:          System.out.printf( "%.1f ", doubleValue );
 31: 
 32:       System.out.print( "\nintArray: " );
 33:       for ( int intValue : intArray )
 34:          System.out.printf( "%d ", intValue );
 35: 
 36:       System.out.print( "\nfilledIntArray: " );
 37:       for ( int intValue : filledIntArray )
 38:          System.out.printf( "%d ", intValue );
 39: 
 40:       System.out.print( "\nintArrayCopy: " );
 41:       for ( int intValue : intArrayCopy )
 42:          System.out.printf( "%d ", intValue );
 43: 
 44:       System.out.println( "\n" );
 45:    } // end method printArrays
 46: 
 47:    // find value in array intArray
 48:    public int searchForInt( int value )
 49:    {  
 50:       return Arrays.binarySearch( intArray, value );
 51:    } // end method searchForInt
 52: 
 53:    // compare array contents
 54:    public void printEquality()
 55:    {
 56:       boolean b = Arrays.equals( intArray, intArrayCopy );
 57:       System.out.printf( "intArray %s intArrayCopy\n",
 58:          ( b ? "==" : "!=" ) );
 59: 
 60:       b = Arrays.equals( intArray, filledIntArray );
 61:       System.out.printf( "intArray %s filledIntArray\n", 
 62:          ( b ? "==" : "!=" ) );
 63:    } // end method printEquality
 64: 
 65:    public static void main( String args[] )
 66:    {
 67:       UsingArrays usingArrays = new UsingArrays();
 68: 
 69:       usingArrays.printArrays();
 70:       usingArrays.printEquality();
 71: 
 72:       int location = usingArrays.searchForInt( 5 );
 73:       if ( location >= 0 )
 74:          System.out.printf( 
 75:             "Found 5 at element %d in intArray\n", location ); 
 76:       else
 77:          System.out.println( "5 not found in intArray" ); 
 78: 
 79:       location = usingArrays.searchForInt( 8763 );
 80:       if ( location >= 0 )
 81:          System.out.printf( 
 82:             "Found 8763 at element %d in intArray\n", location ); 
 83:       else
 84:          System.out.println( "8763 not found in intArray" ); 
 85:    } // end main
 86: } // end class UsingArrays
 87: 
 88: /**************************************************************************
 89:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 90:  * Pearson Education, Inc. All Rights Reserved.                           *
 91:  *                                                                        *
 92:  * DISCLAIMER: The authors and publisher of this book have used their     *
 93:  * best efforts in preparing the book. These efforts include the          *
 94:  * development, research, and testing of the theories and programs        *
 95:  * to determine their effectiveness. The authors and publisher make       *
 96:  * no warranty of any kind, expressed or implied, with regard to these    *
 97:  * programs or to the documentation contained in these books. The authors *
 98:  * and publisher shall not be liable in any event for incidental or       *
 99:  * consequential damages in connection with, or arising out of, the       *
100:  * furnishing, performance, or use of these programs.                     *
101:  *************************************************************************/