Consider

public static void main(String[] args) {

    int[][] foo = new int[][] {
        new int[] { 1, 2, 3 },
        new int[] { 1, 2, 3, 4},
    };

    System.out.println(foo.length); //2
    System.out.println(foo[0].length); //3
    System.out.println(foo[1].length); //4
}

Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.

Answer from NG. on Stack Overflow
🌐
Coding Rooms
codingrooms.com › blog › 2d-array-length-java
https://www.codingrooms.com/blog/2d-array-length-java
package exlcode; public class ... 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

2d Array length rows and column length (Java) - Stack Overflow
NOTE : I am asking for an explanation for Why example.length is used to get the length of row? not how to get the length of columns and row in 2d array? . ... Because every Java array has a fixed length... More on stackoverflow.com
🌐 stackoverflow.com
java - 2D array row and column length - Stack Overflow
I'm studying 2D array right now, there is a part of 2D array I don't really understand. I will show my code and explain what part I don't understand. My code: public static void main(String[] arg... More on stackoverflow.com
🌐 stackoverflow.com
java - Getting the length of two-dimensional array - Stack Overflow
But be aware that there's no real two-dimensional array in Java. Each "first level" array contains another array. Each of these arrays can be of different sizes. nir[0].length isn't necessarily the same size as nir[1].length. ... look at this for loop which print the content of the 2 dimension array the second loop iterate over the column ... More on stackoverflow.com
🌐 stackoverflow.com
January 16, 2021
How to get rows and columns count of a 2D array in Java? - Stack Overflow
For an array in Java, we can get the length of the array with array_name.length. Similarly, how would one get the number of rows and columns of a 2D array? More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › Array2dBasics › a2dLoop.html
10.6. Getting the Number of Rows and Columns — AP CSA Java Review - Obsolete
Since for the AP CS A exam all two-dimensional arrays are rectangular arrays (arrays that have the same number of columns in each row) you can just use the length of the first inner array as the number of columns as shown by ticketInfo[0].length. ... 9-3-1: How many rows does a have if it is ...
🌐
Devcubicle
devcubicle.com › java-2d-array-length
How to get the length of 2d array in Java - DevCubicle By Cloud Tech
December 29, 2019 - Length of 2d array in Java is number of rows present in it, but the number of columns may vary per row. For size of two dimensional array use matrix.length.
Find elsewhere
🌐
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 - Following example helps to determine the rows and columns of a two-dimensional array with the use of arrayname.length. Following example helps to determine the upper bound of a two dimensional array with the use of arrayname.length.
🌐
JavaBeat
javabeat.net › get-length-2d-array-java
How to Get the Length of a 2d Array in Java?
January 30, 2024 - To determine the length of a non-primitive 2D array, create an object array of fixed rows and columns. The “objArray.length” returns the row count of the array. As it is a fixed-size array, the number of columns per row remains the same.
🌐
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 ...
🌐
EXLskills
exlskills.com › courses › java basics › java basics
2D Array Length | Java Basics - EXLskills
August 1, 2018 - When calling the length of a column, we pinpoint the row before using .length. The program above checks to see how many columns the first row of the 2D array contains by calling exampleVariableOne[0].length. Adjust the '0' to another number to change the row specified.
🌐
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 - This method provides a comprehensive ... 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. Although it doesn’t have a direct method to get the length of a 2D array, it can be ...
🌐
automateNow
automatenow.io › home › java 2d array length: 2d array examples
Java 2D array length: 2D array examples | automateNow
May 3, 2024 - That shows that we’re trying to access the array element located on the third row and first column, which produces the following result: ... To get the length of a 2D array in Java, you can use the length property of the array.
🌐
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; ...
🌐
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
Answer (1 of 2): Calculating the size of a 2D array is relatively uncomplicated in Java (and like programming languages): int numberOfColumns = arr[0].length; int numberOfRows = arr.length; Check out the explanation over here: https://www.codingrooms.com/blog/2d-array-length-java/
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - We can calculate the total size of this 2D array as follows: Total size = align(12 bytes + 4 bytes + number of lines × 4 bytes) + number of rows × align(12 bytes + 4 bytes + number of columns × size of element type in bytes)
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