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

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 - 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
Java 2D array length - Stack Overflow
All rows have same columns that is 4 so you get same result with array[1].length and array[2].length. ... Ah, I see I see. Thanks for clarifying!! 2018-10-04T06:48:50.697Z+00:00 ... Save this answer. ... Show activity on this post. Two dimensional array looks like a matrix and is just an array ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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; ...
🌐
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.
🌐
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....
🌐
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.
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.
🌐
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.
🌐
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?

🌐
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 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 ...
🌐
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, ...
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
Top answer
1 of 5
3

The way you should look at it is that you actually make an array of an array of ints. For example you could initialize it like this:

int[][] array = {
    {1, 2, 3, 4}, // This is the first array in the multidimensional array
    {5, 6, 7, 8}, // This is the second array in the multidimensional array
    {9, 10, 11, 12} // This is the third array in the multidimensional array
};

Note that each of these individual arrays inside the multidimensional array has length 4.

So when you ask the length like this:

array.length // = 3

what Java does is give you the number of int[] in the array. Now every array in this array has length 4, hence:

array[0].length // = 4

Also let's say you wanted to access the element with value 7. This element can be found in the second array on the third place. Since Java indexing starts at 0, you can access that element as follows.

array[1][2] // value of this element is 7

You can read more here https://www.programiz.com/java-programming/multidimensional-array. Multidimensional arrays are hard in the beginning, but once you get them, you will use them often.

2 of 5
1

you can think of the following array variable as array of arrays, it consists of 3 arrays each of which contain 4 elements

int [][]array = new int[3][4];

so when you try array.length you get 3 the first array that contains 3 arrays which contain 4 elements each, however when you array[0].length,array[1].length, array[2].length, you are checking lengths of arrays of elements in array variable, which all give you 4.

Edit:

lets say you want to take out the arrays from array variable array and check lengths

int [] arr1 = array[0]; // arr1.length = array[0].length which is 4
int [] arr2 = array[1]; // arr2.length = array[1].length which is 4
int [] arr3 = array[2];  //arr3.length = array[2].length which is 4
🌐
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.
🌐
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 ...
🌐
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.
🌐
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 ...
🌐
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 ·