Two Dimensional Arrays

This chapter expands our discussion on one-dimensional arrays to two dimensional arrays. A two dimensional array is a data structure that contains a collection of cells laid out in a two dimensional grid, similar to a table with rows and columns although the values are still stored linearly in memory. Each cell in a two dimensional array can be accessed through two indexes that specify the row number and column number respectively. Like s one dimensional array, the range of each index is from zero to the size of the row or column minus one. A nested for loop is commonly used to initialize, manipulate, and access the values in a two dimensional array.

10. Length of each Row


Answer:

Length is: 3

Length of each Row

The length of a 2D array is the number of rows it has. The row index runs from 0 to length-1.

Each row of a 2D array can have a different number of cells, so each row has its own length:

uneven[0] refers to row 0 of the array, uneven[1] refers to row 1, and so on.


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 } };

    // length of the array (number of rows)
    System.out.println("Length of array is: " + uneven.length );

    // length of each row (number of its columns)
    System.out.println("Length of row[0] is: " + uneven[0].length );
    System.out.println("Length of row[1] is: " + uneven[1].length );
    System.out.println("Length of row[2] is: " + uneven[2].length );
  }
}


Question 10:

What does the program print?

  • Length of array is:  
  • Length of row[0] is: 
  • Length of row[1] is: 
  • Length of row[2] is: