which 3?

You've created a multi-dimentional array. nir is an array of int arrays; you've got two arrays of length three.

System.out.println(nir[0].length); 

would give you the length of your first array.

Also worth noting is that you don't have to initialize a multi-dimensional array as you did, which means all the arrays don't have to be the same length (or exist at all).

int nir[][] = new int[5][];
nir[0] = new int[5];
nir[1] = new int[3];
System.out.println(nir[0].length); // 5
System.out.println(nir[1].length); // 3
System.out.println(nir[2].length); // Null pointer exception
Answer from Brian Roach on Stack Overflow
🌐
Coding Rooms
codingrooms.com › blog › 2d-array-length-java
2D Array Length Java
We use arrayname.length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has.
🌐
W3Docs
w3docs.com › java
Getting the array length of a 2D array in Java
To get the length of a 2D array in Java, you can use the length field of the array. For example: int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int rows = array.length; // gets the number of rows (3 in this case) int columns = array[0].length; ...
🌐
Coderanch
coderanch.com › t › 402286 › java › dimensional-array-lengths
Two-dimensional array lengths???? (Beginning Java forum at Coderanch)
For example, if "myArray" represents a 2-d array, then the array at myArray[0] is not necessarily the same length as the array at myArray[1]... int[][] myArray = { {1}, {2, 3, 4}, {5, 6} }; "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org ... Originally posted by marc weber: Keep in mind that a 2-d array does not need to be "rectangular," and could contain arrays of different lengths.
🌐
Sololearn
sololearn.com › en › Discuss › 1043050 › what-will-happen-if-you-use-length-function-on-2d-array-
What will happen if you use length function on 2-D Array.. ??? | Sololearn: Learn to code for FREE!
So is the case with 2D arrays, as the array contains 2 elements (the two arrays are treated as 2 different elements), the number returned is 2. Take this as an example, int[][] arr = {{2, 4, 8}, {1, 3, 5}}; System.out.print(arr.length); This ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 319842 › array-length-in-2d-arrays
java - array.length in 2d arrays [SOLVED] | DaniWeb
array.length in 2d array returns the number of rows you entred but if you want to get the number of coloum, you must specify at first which row you want to get the length of it Ex.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › how to get the length of a 2d array in java
How to Get the Length of a 2D Array in Java | Delft Stack
March 11, 2025 - Number of rows: 3 Number of columns in row 0: 2 Number of columns in row 1: 3 Number of columns in row 2: 4 · In this example, we create a jagged array named jaggedArray. First, we retrieve the number of rows using the length property. Then, we loop through each row to determine its length. This method provides a comprehensive view of the structure of a jagged array, allowing you to handle cases where the number of columns varies from one row to another. Java provides a utility class called Arrays that contains various methods for working with arrays.
🌐
JavaBeat
javabeat.net › home › how to get the length of a 2d array in java?
How to Get the Length of a 2d Array in Java?
January 30, 2024 - To calculate the length of 2D arrays, use the “arrayname[i].length” syntax, which will return the length of each column per row as shown in this guide.
🌐
automateNow
automatenow.io › home › java 2d array length: 2d array examples
Java 2D array length: 2D array examples | automateNow
May 3, 2024 - Here is the code for accessing ... the following result: ... To get the length of a 2D array in Java, you can use the length property of the array....
🌐
GeeksforGeeks
geeksforgeeks.org › java › multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
A 3D-array can be seen as an array of 2D array for easier understanding. A three-dimensional array can be seen as a table of arrays with 'x' rows and 'y' columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A three - dimensional array with 3 array containing 3 rows and 3 columns is shown below: ... Example 1: Java ...
Published   November 13, 2025
🌐
Processing Forum
forum.processing.org › one › topic › getting-lengths-of-multidimensional-arrays.html
Getting lengths of multidimensional arrays - Processing Forum
println(what to write here...); ... to get a specific dimension's length printed? eg. i'd like to see "10" printed, or "20" or "30" ... so if you have a=10; b=20; int[][] i = new int[a][b]; i.length will equal a i[0].length will equal b so will i[1].length and so forth use arraylists instead if your data requires three dimensions
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - Only when I reduce the size by two to 2,147,483,645, does it work – and again with all primitive data types, even with long. The limit, therefore, has nothing to do with the available memory but is determined by the VM. The following table shows the maximum array size for all primitive types – both in terms of the number of elements and the heap memory occupied on my VM: The upper limit described in the previous chapter applies to each array dimension.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › a2dSummary.html
8.3. 2D Arrays Summary — CSAwesome v1
For an array arr use arr.length to get the number of rows in the array. 2d Array Number of Columns - The number of columns (or width) is the length of the inner array.
Top answer
1 of 5
9

In order to better understand this, take a look at this image:

This image is what you call 2D array, as you can see, it's actually an array of arrays.

nums.length will return the length of the blue array (which is the number of the rows).
Now if you want to get the number of columns, you should access one row by nums[0] for example, and then do nums[0].length, which will yield 4.

Now, simply replace nums with array...


Note: As you see in the image, the number of columns might differ and it doesn't have to be the same for each row.

2 of 5
7

It's important to understand that Java doesn't really have two-dimensional arrays. It has arrays of arrays. That means, for instance, that you can have this:

int[][] array=
{
    {1},
    {1, 2, 3},
    {1, 2, 3, 4, 5},
    {1, 2}
};

So there is no one upper bound of the second level. Java arrays are inherently jagged, each of the second level in the above has its own length.

So to loop them correctly, you have to check for each of the second-level arrays:

int x, y;
int[] second;

for (x = 0; x < array.length; ++x) {
    second = array[x];
    for (y = 0; y < second.length; ++y) {
         // ....
    }
}

Full example: Live Copy

public class ArrayExample {
    public static void main(String[] args) {
        int[][] array=
        {
            {1},
            {1, 2, 3},
            {1, 2, 3, 4, 5},
            {1, 2}
        };
        int x, y;
        int[] second;

        for (x = 0; x < array.length; ++x) {
          second = array[x];
          for (y = 0; y < second.length; ++y) {
              System.out.println(x + "," + y + ": " + second[y]);
          }
          System.out.println();
        }
    }
}

Output:

0,0: 1

1,0: 1
1,1: 2
1,2: 3

2,0: 1
2,1: 2
2,2: 3
2,3: 4
2,4: 5

3,0: 1
3,1: 2

Or if you don't need the indexes, just the values, you can use the enhanced for loop: Live Example

public class ArrayExample {
    public static void main(String[] args) {
        int[][] array=
        {
            {1},
            {1, 2, 3},
            {1, 2, 3, 4, 5},
            {1, 2}
        };

        for (int[] second : array) {
          for (int entry : second) {
              System.out.println(entry);
          }
          System.out.println();
        }
    }
}

Output:

1

1
2
3

1
2
3
4
5

1
2
🌐
StudySmarter
studysmarter.co.uk › computer science › computer programming › java multidimensional arrays
Java Multidimensional Arrays: Length, Sorting | StudySmarter
To find the length or the number of rows in a Java 2D array, arrayName.length method is used. This method returns the length of the first or outer dimension of the array.
🌐
Carnegie Mellon University
cs.cmu.edu › ~mrmiller › 15-110 › Handouts › arrays2D.pdf pdf
Two-Dimensional Arrays 15-110 Summer 2010 Margaret Reid-Miller
Size of 2D Arrays · • Given · ! !int[][] rating = new int[3][4]; • What is the value of rating.length? • What is the value of rating[0].length? ! Answer: 3, the number of rows (first dimension) Answer: 4, the number of columns (second dimension) 9 · Summer 2010 ·
🌐
Reddit
reddit.com › r/javahelp › two-dimensional arrays: why is the length of a two-dimensional array the number of rows of the array?
r/javahelp on Reddit: Two-Dimensional Arrays: Why is the length of a two-dimensional array the number of rows of the array?
June 29, 2018 -

ETA: Here is an excerpt from the book I'm using to review Java:

...Suppose that x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements...x.length is 3 and x[0].length, x[1].length, and x[2].length are 4.

--

Given that the length of a one-dimensional array is the number of elements, doesn't it make more intuitive sense to apply the same concept to two-dimensional arrays?

🌐
Medium
theonlylight.medium.com › java-101-the-difference-between-array-0-length-and-array-length-36231e4e846c
Java 101: The Difference Between array[0].length and array.length | by Light | Medium
December 12, 2023 - Therefore, array.length returns the number of 1D arrays in the 2D array, which is also the number of rows in the 2D array. On the other hand, array[0].length returns the number of elements in the first 1D array, which is also the number of columns ...