go to previous page   go to home page   go to next page

Answer:


Printing a 2D Array

Here is a program that creates a 2D array, then prints it out.

The nested loops are written to print out the correct number of cells for each row. The expression uneven[row].length evaluates to a different integer for each row of the array.


uneven rows
RowCol
01234
0 1 9 4

1 0 2

2 0 1 2 3 4

class unevenExample3
{
  public static void main( String[] arg )
  {
    // declare and construct a 2D array
    int[][] uneven = 
        { { 1, 9, 4 },
          { 0, 2},
          { 0, 1, 2, 3, 4 } };

    // print out the array
    for ( int row=0; row < uneven.length; row++ )
    {
      System.out.print("Row " + row + ": ");
      for ( int col=0; col < uneven[row].length; col++ )
        System.out.print( uneven[row][col] + " ");
      System.out.println();
    }

  }
}

QUESTION 14:

For the print method to work, must every row of uneven be non-null?


go to previous page   go to home page   go to next page