Tuesday, 16 December 2014

// The "MDArrayExample1B" class.  Pages319-320.
// (from the Carter book)
//
// Example 1B:
//
// 1. A table of int values with 3 rows and 6 columns can be
// created using the declararion (below):

public class MDArrayExample1B
{
    public static void main (String[] args)
    {
        int[] [] table = new int [3] [6];
        // this statement really creates 3 arrays each containing
        // 6 elements which are each an array.
        for (int row = 0 ; row < table.length ; row++)
        {
            for (int col = 0 ; col < table [0].length ; col++)
            {
                table [row] [col] = 1;
                System.out.println (row + ", " + col + " = " + table [row] [col]);
            } // end of inner for loop
        } // end of outer for loop

    } // main method
} // MDArrayExample1B class

// Example 1B: (continued):
//
// 2. Just as a for loop statement is usually used to index through
// a one-dimensional array, a nested for loop statement is usually
// used to index through a 2-dimensional array.  For the array called
// "table" shown above, we can initialize all the elements to 1 using the
// following nested for loop construct (see the nested for loop above).
// 3. Notice the way in which the upper bounds of the loops are written.
// The number of rows in "table" is given by "table.length", while the
// number of columns in row zero (and all other rows) is given by
// "table[0].length".
// 4. Two-dimensional arrays can be initialized as they are being declared,
// in a manner similar to that used for one-dimensional arrays.

No comments:

Post a Comment