Lets start with some definitions:

Let us assume that a 3 x 4 rectangular array has 3 columns and 4 rows and is represented by:

boolean landscape[][] = new boolean[3][4];

To get the number of rows and columns:

int nosRows = landscape[0].length;  // 4
int nosCols = landscape.length;     // 3

If you think I've got rows and columns the wrong way around in my terminology, mentally rename them. There is no universal convention about which dimension represents the columns and which represents the rows in Java code.

If you are expecting some answer other than 3 or 4, you will need to explain what you are talking about.


Obviously, this only works for square or rectangular arrays.

Answer from Stephen C on Stack Overflow
Top answer
1 of 7
23

Lets start with some definitions:

Let us assume that a 3 x 4 rectangular array has 3 columns and 4 rows and is represented by:

boolean landscape[][] = new boolean[3][4];

To get the number of rows and columns:

int nosRows = landscape[0].length;  // 4
int nosCols = landscape.length;     // 3

If you think I've got rows and columns the wrong way around in my terminology, mentally rename them. There is no universal convention about which dimension represents the columns and which represents the rows in Java code.

If you are expecting some answer other than 3 or 4, you will need to explain what you are talking about.


Obviously, this only works for square or rectangular arrays.

2 of 7
6

So 2D arrays are really just arrays of arrays. So if I have a 2D array with 5 rows and 4 columns, you could think of it as an array containing 5 subarrays that have 4 spots each.

boolean landscape[][] = new boolean[5][4];

int numRows = landscape.length;
int numCols = landscape[0].length;

System.out.println("Number of rows: " + numRows);
System.out.println("Number of cols: " + numCols);

for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
        System.out.print(landscape[i][j]);
    }
    System.out.println("");
}

Output:

Number of rows: 5
Number of cols: 4
0000
0000
0000
0000
0000

It is important to note that java is considered row major (the row always comes first): Which comes first in a 2D array, rows or columns? This is important because if you iterate column first (column 0: row 0, row 1, row 2, row 3, row 4; column 1: row 0, row 1, etc.), you are actually going through the 2D array inefficiently, because the memory is stored consecutively row by row, so doing it column by column means you are skipping over segments of memory and then going back, instead of just going consecutively; read about assemblers and compilers for more info.

🌐
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

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
How to ask 2-dimensional Java array for its number of rows? - Stack Overflow
How should I go about asking a 2-dimensional array how many rows it has? More on stackoverflow.com
🌐 stackoverflow.com
October 20, 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
Is it possible to get just the number of rows from a 2D array?
LabVIEW does this automatically for you! Just wire your 2D array directly to the for loop (not the N). The "tunnel icon" that appears will look like an array bracket. If it doesn't, and looks like a solid block right click on it. The first option on the option box will be "enable indexing", select that. The for loop will now automatically iterate through every row. Also the array size function is giving you the number of rows and columns. Use the "index array" function to select the size of each dimension. More on reddit.com
🌐 r/LabVIEW
12
5
March 31, 2022
🌐
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
Arrays know their length (how many elements they can store). It is a public read-only field so you can use dot-notation to access the field (arrayName.length). The length of the outer array is the number of rows and the length of one of the inner arrays is the number of columns.
🌐
CodeGym
codegym.cc › java blog › java arrays › 2d arrays in java
Java Matrix - 2D Arrays
April 8, 2025 - For example, if you specify an integer array int arr[4][4] then it means the matrix will have 4 rows and 4 columns. 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 ...
🌐
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.
Find elsewhere
🌐
Gatech
ice-web.cc.gatech.edu › ce21 › 1 › static › JavaReview-RU › Array2dBasics › a2dLoop.html
Getting the Number of Rows and Columns — Java Review
Arrays know their length (how many elements they can store). It is a public read-only field so you can use dot-notation to access the field (arrayName.length). The length of the outer array is the number of rows and the length of one of the inner arrays is the number of columns.
🌐
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)

🌐
HWS
math.hws.edu › eck › cs124 › javanotes8 › c7 › s5.html
Javanotes 8.1.3, Section 7.5 -- Two-dimensional Arrays
Two-dimensional arrays were introduced in Subsection 3.8.5, but we haven't done much with them since then. A 2D array has a type such as int[][] or String[][], with two pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of 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.
🌐
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 ...
🌐
HWS Math
math.hws.edu › javanotes › c7 › s6.html
Javanotes 9, Section 7.6 -- Two-dimensional Arrays
Two-dimensional arrays were introduced in Subsection 3.8.5, but we haven't done much with them since then. A 2D array has a type such as int[][] or String[][], with two pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns.
🌐
Physics Forums
physicsforums.com › other sciences › programming and computer science
How to Count Rows and Columns in a Two-Dimensional Array in Java? • Physics Forums
December 8, 2007 - I just have a question about my "solution" to a problem. The question says to write a value returning method that returns the number of rows in a two-dimensional array of doubles. It should be simple enough and so I did it like this: public int countRows() { int x; x = 0; for (int...
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › topic-8-1-2D-arrays-Day1.html
8.1.1. 2D Arrays (Day 1) — CSAwesome v1
Figure 2: A 2D array stored in row-major order or column-major order as a 1D array.¶ · Java actually stores a two-dimensional array as an array of arrays. Each element of the outer array has a reference to each inner array. The picture below shows a 2D array that has 3 rows and 7 columns.
🌐
automateNow
automatenow.io › home › java 2d array length: 2d array examples
Java 2D array length: 2D array examples | automateNow
May 3, 2024 - Each “slice” of the stack represents a 2D array, and the stack itself adds a third dimension, forming a three-dimensional grid of elements. 3D arrays are particularly useful when dealing with volumetric data or spatial arrangements. Here are some scenarios where a 3D array could be used: ... Extending the concept to higher dimensions, getting the length of a 3D array involves accessing the length of each dimension. For example, to get the number of rows, columns, and depth in a 3D array:
🌐
Minich
minich.com › education › wyo › java › lecture_notes › two_dimensional_arrays.php
Objective #1: Declare and instantiate two-dimensional arrays.
The following statement declares & instantiates a two-dimensional array of integers named grades that has 3 rows and 4 columns. int[][] grades = new int[3][4]; The first set of square brackets represents the length of the one-dimensional array (i.e. the number of rows) and the second set of ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit9-2DArray › a2dSummary.html
9.3. 2D Arrays Summary — CS Java
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.