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
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
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
🌐
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.
🌐
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. Example Following example helps to determine the upper bound of a two dimensional array
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
🌐
javathinking
javathinking.com › blog › getting-the-array-length-of-a-2d-array-in-java
How to Get Row and Column Lengths of a 2D Array in Java: Explaining `test[0].length` — javathinking.com
If you’ve ever encountered code ... to get the number of rows and columns in a 2D array, explain why `test[0].length` is used for columns, and cover edge cases like jagged arrays. By the end, you’ll confidently handle 2D array dimensions in Java....
Find elsewhere
🌐
Devcubicle
devcubicle.com › java-2d-array-length
How to get the length of 2d array in Java - DevCubicle By Cloud Tech
December 29, 2019 - Let’s have a look at the program ... 75, 33 }, { 47, 21, 23, 7, 19 }, { 66, 91, 15, 18, 3 } }; System.out.println("Length of 2d Array is : " + elements.length); // 3 System.out.println("*Size of*"); System.out.println("1st ...
🌐
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.
🌐
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; ...
🌐
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.");...
🌐
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.
🌐
Gatech
ice-web.cc.gatech.edu › ce21 › 1 › static › JavaReview-RU › Array2dBasics › a2dLoop.html
Getting the Number of Rows and Columns — Java Review
Since you can find out the number of rows and columns in a 2D array you can use a nested for loop (one loop inside of another loop) to loop through all of the elements of a 2D array. public static double getAverage(int[][] a) { double total = 0; for (int row = 0; row < a.length; row++) { for ...
🌐
Reddit
reddit.com › r/learnjavascript › how do i find the number of rows and columns of a 2d array?
r/learnjavascript on Reddit: How do I find the number of rows and columns of a 2D array?
September 8, 2021 -

Edit: solved. It is the same as Java as someone pointed out

In java it would be

grid.length for number of rows and grid[i].length for number of columns (the code is in a nested for loop)

🌐
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 - For example, if you want to create ... One of the simplest methods to get the length of a 2D array in Java is by utilizing the length property....
🌐
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.
🌐
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.