Source of PassArray.java


  1: // Fig. 7.13: PassArray.java
  2: // Passing arrays and individual array elements to methods.
  3: 
  4: public class PassArray 
  5: {
  6:    // main creates array and calls modifyArray and modifyElement
  7:    public static void main( String args[] )
  8:    {
  9:       int array[] = { 1, 2, 3, 4, 5 };
 10:       
 11:       System.out.println( 
 12:          "Effects of passing reference to entire array:\n" +
 13:          "The values of the original array are:" );
 14: 
 15:       // output original array elements 
 16:       for ( int value : array )
 17:          System.out.printf( "   %d", value );
 18:    
 19:       modifyArray( array ); // pass array reference
 20:       System.out.println( "\n\nThe values of the modified array are:" );
 21: 
 22:       // output modified array elements 
 23:       for ( int value : array )
 24:          System.out.printf( "   %d", value );
 25:    
 26:       System.out.printf( 
 27:          "\n\nEffects of passing array element value:\n" +
 28:          "array[3] before modifyElement: %d\n", array[ 3 ] );
 29:    
 30:       modifyElement( array[ 3 ] ); // attempt to modify array[ 3 ]
 31:       System.out.printf( 
 32:          "array[3] after modifyElement: %d\n", array[ 3 ] );
 33:    } // end main
 34:    
 35:    // multiply each element of an array by 2 
 36:    public static void modifyArray( int array2[] )
 37:    {
 38:       for ( int counter = 0; counter < array2.length; counter++ )
 39:          array2[ counter ] *= 2;
 40:    } // end method modifyArray
 41:    
 42:    // multiply argument by 2
 43:    public static void modifyElement( int element )
 44:    {
 45:       element *= 2;
 46:       System.out.printf( 
 47:          "Value of element in modifyElement: %d\n", element );
 48:    } // end method modifyElement  
 49: } // end class PassArray
 50: 
 51: 
 52: /**************************************************************************
 53:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 54:  * Pearson Education, Inc. All Rights Reserved.                           *
 55:  *                                                                        *
 56:  * DISCLAIMER: The authors and publisher of this book have used their     *
 57:  * best efforts in preparing the book. These efforts include the          *
 58:  * development, research, and testing of the theories and programs        *
 59:  * to determine their effectiveness. The authors and publisher make       *
 60:  * no warranty of any kind, expressed or implied, with regard to these    *
 61:  * programs or to the documentation contained in these books. The authors *
 62:  * and publisher shall not be liable in any event for incidental or       *
 63:  * consequential damages in connection with, or arising out of, the       *
 64:  * furnishing, performance, or use of these programs.                     *
 65:  *************************************************************************/