HOWTO · Java

How to Get the Length of a 2D Array in Java

Learn how to get a Java 2D array's length, find rectangular dimensions, inspect jagged row lengths, and handle empty or null rows safely.

On this page

For a Java 2D array named arr, arr.length gives the number of rows. For a rectangular array, arr[0].length gives the number of columns in each row, provided the outer array has a first row. Java represents a 2D array as an array whose elements are separate row arrays, so a jagged array can have different lengths in different rows.

How Java 2D Arrays Store Rows and Columns

A declaration such as int[][] values describes an outer array of int[] rows. The outer array’s length counts row references; it does not count every integer in the grid. Each row has its own length. A rectangular array is one where all rows have the same length. A jagged array is one where row lengths can differ.

The length member is a field of every Java array, not a method. Write values.length without parentheses. To inspect a particular row, use values[rowIndex].length.

Get the Dimensions of a Rectangular 2D Array

Use the outer array’s length for the row count. If the array has at least one row and every row has the same size, the first row’s length is the column count. This complete example prints both dimensions:

Run this example:

class Main {
    public static void main(String[] args) {
        int[][] scores = {
            {8, 7, 9},
            {6, 5, 8}
        };

        int rows = scores.length;
        int columns = scores[0].length;
        System.out.println("Rows: " + rows);
        System.out.println("Columns: " + columns);
    }
}

The output shows two rows and three columns, matching the dimensions of scores:

Rows: 2
Columns: 3

Get the Length of Each Row in a Jagged Array

There is no single column count for a jagged array. Select a row to get its length, or loop over the outer array to inspect every row. The loop condition uses rows.length, while rows[i].length reports the selected row’s size:

class Main {
    public static void main(String[] args) {
        int[][] rows = {
            {4, 2},
            {7, 1, 3},
            {9}
        };

        System.out.println("Rows: " + rows.length);
        for (int i = 0; i < rows.length; i++) {
            System.out.println("Row " + i + " length: " + rows[i].length);
        }
    }
}

The output reports each row’s length; if you only need one row, use an expression such as rows[1].length:

Rows: 3
Row 0 length: 2
Row 1 length: 3
Row 2 length: 1

Handle Empty or Null Rows Safely

Before reading arr[0].length, ensure the outer array contains a row. An array created as new int[0][] has no first row, so check arr.length > 0 before using index 0. For a requested row, first verify rowIndex >= 0 && rowIndex < arr.length; Java array indexes start at zero, and the last valid index is one less than arr.length. Then check that arr[rowIndex] is not null before reading its length, because dereferencing a null row causes a NullPointerException. An existing row with no elements is different: its length is 0 and can be read safely.

class Main {
    public static void main(String[] args) {
        int[][] arr = { {1, 2}, null, {} };

        System.out.println("Rows: " + arr.length);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                System.out.println("Row " + i + " is null");
            } else {
                System.out.println("Row " + i + " length: " + arr[i].length);
            }
        }
    }
}

This example produces:

Rows: 3
Row 0 length: 2
Row 1 is null
Row 2 length: 0

The null row is identified without dereferencing it, while the allocated row with no elements has length zero. If the outer array itself may be null, check arr != null before reading arr.length.