I have this 2d array:
private boolean[][] landscape;
this landscape array defines a pond with its [rows] and [cols]
The method, public int getRows()
, needs to return the number of rows in the landscape array
I tried just return landscape.length;
and that didnt work. Couple more things I tried without success:
int count = 0;
for (boolean[] i : landscape){
count += i.length;
}
return count;
And
int count = 0;
开发者_C百科 for (int i = 0; i < landscape.length; i++) {
if (landscape[i] != null){
count ++;
}
}
return count;
The amount of rows and cols depends on what the user selects I believe. There is a minimum of 5. So how would i do this?
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.
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.
Let's consider a matrix:
0 1 0
1 0 1
1 0 0
0 0 1
We can represent this matrix as a 2D array in java with 4 rows and 3 columns.
Initialization:
int[][] matrix=new int[4][3];
In java, 2D arrays are not entirely row major,but an array of arrays. But to answer your question and explain you in an easier way, we can see this as a row-major arrangement.
To make things more clear, matrix[1]
points to a row at index 1. matrix[1]
is actually an array. Its length is equal to the number of columns in this matrix.
So, matrix[1].length
would give us the number of columns. Here, for this example index being used is not relevant until it is not out of bounds. We could have also used 0 as the index.
To get the number of rows we need to get the value of length on array variable which gives us the number of rows.
In essence,
int row = matrix.length; //4 for above example
int col = matrix[0].length; //3 for above example
I hope, I am able to answer your question.
How about return landscape[0].length
? You'd want to be sure the array element at landscape[0]
was initialised already, but it sounds like that'd be fine.
See, since (I assume) you're initialising as landscape[rows][cols]
, asking for landscape.length
gets you the number of cols
- it works from the outside in. You need to go deeper.
Your code should work.
int count = 0; for (int i = 0; i < landscape.length; i++) { if (landscape[i] != null){ count ++; } } return count;
Can you post your code to how you populate your 2D array?
Here is a project I did that counted rows and elements in my 2D array.
double[][] array = {{1,2,3,3,3,3},{3,4,5,8,9},{8,9,1}};
// counter to count the amount of elements in the 2d array
double elements = 0; // 14 elements in array above 1,2,3,3,3,3,3,4,5,8,9,8,9,1
// counter to count the amount of rows.
double rows = 0; // 3 rows in the array above. {}{}{}
// loop through rows
for (int i = 0; i < array.length; i++) {
rows++;
// loop through columns
for (int j = 0; j < array[i].length; j++) {
elements++;
}
}
Stephen, your code has a slight error according to general convention. According to standard convention, the following should give you the correct values of rows and columns:
int rows = matrix.length;
int cols = matrix[0].length;
Also, this assumes that you already have the array populated and are checking the number of rows and columns at the end.
Its quite simple just use below code to get rows and columns in the matrix without running any loop.
int rows = M.length;
int columns = M[0].length;
精彩评论