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
🌐
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 - So whenever you'll access the length variable of any multidimensional array it will return the value that was passes in the 1st Square Brackets while the creation of the array. 21st Feb 2018, 6:08 PM · Harshit Upadhyay · Answer · Learn more efficiently, for free: Introduction to Python · 7.1M learners · Introduction to Java ·
Discussions

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
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
I want to know the length of a two-dimensional array
Assuming that every row has the same amount of elements you can get the length of the array with len(array) to get the amount of rows, and then get the number of columns by checking the length of the first row len(array[0]). Example array = [[1, 2, 3],[1, 2, 3]] print(len(array), len(array[0]) # outputs 2, the number of rows, and 3, the number of columns More on reddit.com
🌐 r/AskProgramming
6
0
December 24, 2021
🌐
Quora
quora.com › How-do-I-find-the-length-of-two-dimensional-arrays
How to find the length of two dimensional arrays - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
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.
🌐
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?

🌐
Saylor Academy
learn.saylor.org › mod › book › view.php
Two Dimensional Arrays: Length of each Row
Privacy Policy Terms of Use · © Saylor University 2010-2026 except as otherwise noted. Excluding course final exams, content authored by Saylor University is available under a Creative Commons Attribution 3.0 Unported license. Third-party materials are the copyright of their respective owners ...
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 - Learn how to get the length of a 2D array in Java with this comprehensive guide. Explore various methods, including using the length property, iterating through the array, and utilizing the Arrays utility class. This article is designed to help you understand and apply these techniques effectively ...
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
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-rows-and-columns-of-2d-array-in-java
How to get rows and columns of 2D array in Java?
February 24, 2020 - Python TechnologiesDatabasesComputer ProgrammingWeb DevelopmentJava TechnologiesComputer ScienceMobile DevelopmentBig Data & AnalyticsMicrosoft TechnologiesDevOpsLatest TechnologiesMachine LearningDigital MarketingSoftware QualityManagement Tutorials View All Categories ... Following example helps to determine the rows and columns of a two-dimensional array with the use of arrayname.length.
🌐
Quora
quora.com › What-is-the-formula-to-calculate-the-size-of-a-2D-array
What is the formula to calculate the size of a 2D array? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
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.
🌐
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.
🌐
Coderanch
coderanch.com › t › 402286 › java › dimensional-array-lengths
Two-dimensional array lengths???? (Beginning Java forum at Coderanch)
For exanmple if i have [x][y], how do i find the legnth of x? the .length method seems to give me the length of y. Cheers [ February 06, 2006: Message edited by: Sam Bluesman ] ... A two-dimensional array in Java is just an array of arrays. Try this: ... Keep in mind that a 2-d array does not need to be "rectangular," and could contain arrays of different lengths.
🌐
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 ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 319842 › array-length-in-2d-arrays
java - array.length in 2d arrays [SOLVED] | DaniWeb
November 22, 2010 - 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.
🌐
Vaia
vaia.com › java multidimensional arrays
Java Multidimensional Arrays: Definition, Creation - Vaia
For arrays with dynamic or irregular row sizes, consider using lists instead to manage varying data length more gracefully. Definition of Java Multidimensional Arrays: Arrays of arrays, structured for storing data in multiple dimensions, commonly 2D (grids/tables) or 3D.