Source of InitArray.java


  1: // Fig. 7.17: InitArray.java
  2: // Initializing two-dimensional arrays.
  3: 
  4: public class InitArray 
  5: {
  6:    // create and output two-dimensional arrays
  7:    public static void main( String args[] )
  8:    {
  9:       int array1[][] = { { 1, 2, 3 }, { 4, 5, 6 } };        
 10:       int array2[][] = { { 1, 2 }, { 3 }, { 4, 5, 6 } }; 
 11: 
 12:       System.out.println( "Values in array1 by row are" );
 13:       outputArray( array1 ); // displays array1 by row
 14:    
 15:       System.out.println( "\nValues in array2 by row are" );
 16:       outputArray( array2 ); // displays array2 by row
 17:    } // end main
 18: 
 19:    // output rows and columns of a two-dimensional array
 20:    public static void outputArray( int array[][] )
 21:    {
 22:       // loop through array's rows
 23:       for ( int row = 0; row < array.length; row++ ) 
 24:       {
 25:          // loop through columns of current row
 26:          for ( int column = 0; column < array[ row ].length; column++ )
 27:             System.out.printf( "%d  ", array[ row ][ column ] );
 28: 
 29:          System.out.println(); // start new line of output
 30:       } // end outer for 
 31:    } // end method outputArray
 32: } // end class InitArray
 33: 
 34: 
 35: /**************************************************************************
 36:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 37:  * Pearson Education, Inc. All Rights Reserved.                           *
 38:  *                                                                        *
 39:  * DISCLAIMER: The authors and publisher of this book have used their     *
 40:  * best efforts in preparing the book. These efforts include the          *
 41:  * development, research, and testing of the theories and programs        *
 42:  * to determine their effectiveness. The authors and publisher make       *
 43:  * no warranty of any kind, expressed or implied, with regard to these    *
 44:  * programs or to the documentation contained in these books. The authors *
 45:  * and publisher shall not be liable in any event for incidental or       *
 46:  * consequential damages in connection with, or arising out of, the       *
 47:  * furnishing, performance, or use of these programs.                     *
 48:  *************************************************************************/