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 OverflowLets 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.
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.
How to get rows and columns count of a 2D array in Java? - Stack Overflow
How to ask 2-dimensional Java array for its number of rows? - Stack Overflow
How do I find the number of rows and columns of a 2D array?
Is it possible to get just the number of rows from a 2D array?
Videos
Firstly, Java technically doesn't have 2-dimensional arrays: it has arrays of arrays. So in Java you can do this:
String arr[][] = new String[] {
new String[3],
new String[4],
new String[5]
};
The point I want to get across is the above is not rectangular (as a true 2D array would be).
So, your array of arrays, is it by columns then rows or rows then columns? If it is rows then columns then it's easy:
int rows = arr.length;
(from the above example).
If your array is columns then rows then you've got a problem. You can do this:
int rows = arr[0].length;
but this could fail for a number of reasons:
- The array must be size 0 in which case you will get an exception; and
- You are assuming the length of the first array element is the number of rows. This is not necessarily correct as the example above shows.
Arrays are a crude tool. If you want a true 2D object I strongly suggest you find or write a class that behaves in the correct way.
Object[][] data = ...
System.out.println(data.length); // number of rows
System.out.println(data[0].length); // number of columns in first row
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)