Length of each Row

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

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:

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

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

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: