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
🌐
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 ...
Discussions

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 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... More on stackoverflow.com
🌐 stackoverflow.com
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
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
Java 2D array length - Stack Overflow
I made a very basic 2D Array that has 3 rows and 4 columns like this. ... I get a result of 3. However, when I try: ... I get a result of 4. Same thing using array[1].length, and array[2].length. I'm not sure what the compiler is processing. Can anyone explain? ... I'm not sure what the compiler is processing.: what does that mean? Are you surprised by this result? Why? Note that there is no such thing as 2D arrays in Java... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Coding Rooms
codingrooms.com › blog › 2d-array-length-java
https://www.codingrooms.com/blog/2d-array-length-java
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.
🌐
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.
🌐
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; ...
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
🌐
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.
Find elsewhere
🌐
sebhastian
sebhastian.com › 2d-array-length-java
How to get the length of a 2D array in Java | sebhastian
December 19, 2022 - Here is an example of how to get ... array.length; int numColumns = array[0].length; System.out.println("The array has " + numRows + " rows " + "and " + numColumns + " columns.");...
🌐
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.
🌐
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.
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
🌐
Stack Overflow
stackoverflow.com › questions › 70118155 › how-can-i-get-a-column-from-2d-array-in-java
How can i get a column from 2d array in java - Stack Overflow
array is as like this on top and my method is as follows · public static Object[] getColumn(char[][] arr, int index){ Object[] column = new Object[arr[0].length]; for(int i = 0; i < column.length; i++){ column[i] = arr[i][index]; } return column; }
🌐
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 ...
🌐
Stack Overflow
stackoverflow.com › questions › 35470775 › retrieve-columns-size-of-a-2d-array
java - Retrieve columns size of a 2D array? - Stack Overflow
int[][][] edges = { {{ 0, 1 }, { 0, 3 }, { 0, 5 }}, // row 0/vertex 0 {{ 1, 0 }, { 1, 2 }, { 1, 3 }}, {{ 2, 1 }, { 2, 3 }, { 2, 4 }, { 2, 10 }}, {{ 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 4 }, { 3, 5 }}, {{ 4, 2 }, { 4, 3 }, { 4, 5 }, { 4, 7 }, { 4, 8 }, { 4, 10 }}, {{ 5, 0 }, { 5, 3 }, { 5, 4 }, { 5, 6 }, { 5, 7 }}, {{ 6, 5 }, { 6, 7 }}, {{ 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 8 }}, {{ 8, 4 }, { 8, 7 }, { 8, 9 }, { 8, 10 }, { 8, 11 }}, {{ 9, 8 }, { 9, 11 }}, {{ 10, 2}, { 10, 4 }, { 10, 8 },{ 10, 11 }}, {{ 11, 8 }, { 11, 9 },{ 11, 10 } }}; for(int row = 0; row < edges.length; row++){ System.out.println("Row " + row + ": " + edges[row].length + " columns."); }
🌐
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[0].length to get the number of columns. nested for loop - A for loop inside of another for loop. These are used to loop through all the elements in a 2d array.
🌐
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 - One of the simplest methods to get the length of a 2D array in Java is by utilizing the length property. This property can be used to find out how many rows and columns are present in the array.