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
https://www.codingrooms.com/blog/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.
Discussions

Getting the array length of a 2D array in Java - Stack Overflow
Or maybe better, there is no such thing as a 2D array in Java. More on stackoverflow.com
🌐 stackoverflow.com
java - How to get length of rows and columns in Two-Dimensional array? - Stack Overflow
Please don't use capitals as the first letter of java variable names. ... Especially names that are classes in the JDK! ... Save this answer. ... Show activity on this post. 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 ... More on stackoverflow.com
🌐 stackoverflow.com
Two-Dimensional Arrays: Why is the length of a two-dimensional array the number of rows of the array?
As is hinted at by that snippet, a two-dimensional array is just an array of arrays. .length behaves the same with both — it returns the number of values in the array. In the case of a two-dimensional array, it’s values are one-dimensional arrays. More on reddit.com
🌐 r/javahelp
6
11
June 29, 2018
Java is it possible to change 2D array length?
You've run into one of the first major problems that lead to further investigation into data structures. In most languages, arrays are declared as a fixed size and they can't be changed. The best way to increase the size of an array is to allocate a new array and copy all the values over. It's possible that you might want a different data structure to solve this problem, however. More on reddit.com
🌐 r/learnprogramming
3
2
May 20, 2020
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
🌐
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; ...
🌐
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.
Find elsewhere
🌐
The Revisionist
therevisionist.org › home › software engineering › java › step by step java tutorials › 6.1 java | two-dimensional array basics | 2d array making, finding length & ragged arrays
6.1 Java | Two-Dimensional Array Basics | 2D Array Making, Finding Length & Ragged Arrays - The Revisionist
October 12, 2016 - Each row in a two dimensional array is itself an array. Thus, the rows can have different lengths. An array of this kind is known as a ragged array. Here is an example of making a ragged array: As you can see, triangleArray[0].length is 5, traingleArray[1].length is 4, triangleArray[2] is 3, triangleArray[3] is 2, and triangleArray[4] is 1.
🌐
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....
🌐
JavaBeat
javabeat.net › get-length-2d-array-java
How to Get the Length of a 2d Array in Java?
January 30, 2024 - Determining the total length of the 2D array can be challenging as the number of columns per row can vary. To find the 2D array length, use “arrayname[i].length” which will return the length of each column per row as shown in this guide. This article is a practical guide for finding the ...
🌐
Eclipse Foundation
eclipse.org › community › eclipse_newsletter › 2016 › november › article1.php
Multi-Dimensional Arrays in Java with Eclipse January | The Eclipse Foundation
The data structures were developed and used over the years at Diamond Light Source and Oakridge National Labs, two facilities that have a lot of experience dealing with huge, complex amounts of data, in 2-D, 3-D and multi-dimensional formats. The power of Eclipse January comes not just from the simplicity and convenience of being able to manipulate data, but also provides a basic standard for data storage. This allows for easy integration of tooling based on the Dataset class. So as data sizes continue to grow and be more complex, Eclipse January provides a convenient, powerful, robust library to simplify and standardise multi-dimensional arrays in Java.
🌐
EXLskills
exlskills.com › courses › java basics › java basics
2D Array Length | Java Basics - EXLskills
August 1, 2018 - 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.
🌐
Coderanch
coderanch.com › t › 402286 › java › dimensional-array-lengths
Two-dimensional array lengths???? (Beginning Java forum at Coderanch)
For example, if "myArray" represents ... myArray = { {1}, {2, 3, 4}, {5, 6} }; Which also means that : int[][] myArray = new int[3][]; myArray[0] = new int[1]; myArray[1] = new int[3]; myArray[2] = new int[2]; And you should not forget initialze the arrays...
🌐
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!
January 31, 2018 - Talking about Java, the length function always gives the number of elements in the array. 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 ...
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - As you have seen above, a two-dimensional array is actually an array of arrays. Therefore, we must add the memory space of the outer array and that of all the inner arrays. The array from the example shown above has the following memory layout: We can calculate the total size of this 2D array as follows:
🌐
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.
🌐
YouTube
youtube.com › watch
Java Eclipse zweidimensionales Array ausgeben - Java Tutorial - YouTube
Jetzt Java lernen: http://bit.ly/JavaLernenTypo 3 lernen: http://bit.ly/LerneTypo3In diesem Video geht es wieder um zweidimensionale Arrays. Diesmal schreibe...
Published   October 17, 2016
🌐
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?

🌐
Java67
java67.com › 2014 › 10 › how-to-create-and-initialize-two-dimensional-array-java-example.html
How to declare and Initialize two dimensional Array in Java with Example | Java67
You can also visualize it like a 3 integer arrays of length 2. You can find the number of rows using numbers.length and number of columns using numbers[0].length expression, as shown in the below example. This is also very useful while iterating over a two-dimensional array in Java.
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
2D arrays are declared by defining a data type followed by two sets of square brackets. ... In Java, when accessing the element from a 2D array using arr[first][second], the first index can be thought of as the desired row, and the second index ...