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
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... A multidimensional array is an array that contains other arrays. You can use it to store data in a table with rows and columns. To create a two-dimensional array, write each row inside its own curly braces: ... To access an element of a two-dimensional array, you need two indexes: the first for the row, and the second for the column.
Discussions

Array length question
A two-dimensional array is just an array of arrays. So an expression like grid[0] will get the first 1-D array from the 2-D array, which will be: {'A','B','C','E'}. That array has 4 elements, so its length will be 4. More on reddit.com
๐ŸŒ r/learnjava
5
5
February 22, 2019
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
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
How do I find the number of rows and columns of a 2D array?
What problem are you having? Its identical to the way java does it. More on reddit.com
๐ŸŒ r/learnjavascript
2
2
September 8, 2021
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ multidimensional-arrays-in-java
Java Multi-Dimensional Arrays - GeeksforGeeks
... A 2-D array can be seen as a table with 'x' rows and 'y' columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A 2-D array 'x' with 3 rows and 3 columns is shown below: ... Example 1: We can add the ...
Published ย  May 4, 2026
๐ŸŒ
Coding Rooms
codingrooms.com โ€บ blog โ€บ 2d-array-length-java
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.
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ array-length-in-java
Array Length in Java
June 12, 2025 - The height is the length of the array (i.e., the number of rows the matrix contains), and the width is the length of the first row. Please note, however, that with a two-dimensional array in Java, all sub-arrays do not necessarily have to be ...
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ learn-java โ€บ modules โ€บ java-two-dimensional-arrays โ€บ cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
In Java, nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue. ... In Java, 2D arrays are stored as arrays of arrays.
Find elsewhere
๐ŸŒ
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
medium.com โ€บ @AlexanderObregon โ€บ understanding-multi-dimensional-arrays-in-java-7ead0c3937dd
Understanding Multi-Dimensional Arrays in Java
February 22, 2025 - The matrix.length gives you the number of rows, and matrix[i].length gives you the number of columns in the current row. This pattern of nested loops is common when working with multi-dimensional arrays. Itโ€™s important to note that in Java, each row in a 2D array can have a different number ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ array length question
r/learnjava on Reddit: Array length question
February 22, 2019 -

I've been playing around with arrays lately, and i found a piece of code that i haven't figured, spoiler alert this might be a dumb/basic question but when run this code:

public static void main(String[] args) {

 char grid[][]={{'A','B','C','E'},
			   {'S','F','C','S'},
	           {'A','D','E','E'}};

System.out.println(grid[0].length); //prints 4, WHY?

}

My question is why is grid[0].length = 4?

Is it excluding the 0 position out of the array and considering 1 though 4?

I don't know what's going on to make it print that value.

๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java arrays โ€บ 2d arrays in java
Java Matrix - 2D Arrays
April 8, 2025 - Or you can say for each row there will be 4 columns. The total size / number of cells in a matrix will be rows*columns = mxn = 4x4 = 16. Fig 2: The matrix[4][4] in Fig 1 represented as 2D Array in Java
๐ŸŒ
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.
๐ŸŒ
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/
๐ŸŒ
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.
๐ŸŒ
automateNow
automatenow.io โ€บ home โ€บ java 2d array length: 2d array examples
Java 2D array length: 2D array examples | automateNow
May 3, 2024 - To get the length of a 2D array in Java, you can use the length property of the array.
๐ŸŒ
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 - Talking about Java, the length function always gives the number of elements in the array. 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 ...
๐ŸŒ
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.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 399717 โ€บ java โ€บ multidimensional-array-length
multidimensional array length (Beginning Java forum at Coderanch)
The second one that seems to work is actually calling length() on the object at the cell [x][y] which must be a String or something that has a length() method. Array doesn't have a length() method, it has a length attribute - no parens. You'd have to call the inventors of the language to ask ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ print-2-d-array-matrix-java
Print a 2D Array or Matrix in Java - GeeksforGeeks
March 24, 2025 - // Java program to print the elements of // a 2 D array or matrix import java.io.*; class Geeks { public static void print2D(int mat[][]) { // Loop through all rows for (int i = 0; i < mat.length; i++) // Loop through all elements of current row for (int j = 0; j < mat[i].length; j++) System.out.print(mat[i][j] + " "); } public static void main(String args[]) throws IOException { int mat[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; print2D(mat); } } Output ยท 1 2 3 4 5 6 7 8 9 10 11 12 ยท Complexity of the above method: Time Complexity: O(N*M). Auxiliary Space: O(1) Example 2: Printing the 2D Array using the nested for-each loop. Java ยท