Returns the number of rows in a 2d array?

I have this 2d array:

private boolean[][] landscape;

this landscape array defines a pond with its [lines] and [cols]

The method public int getRows()should return the number of rows in the landscape array

I tried just return landscape.length;and it didn’t work. Just a couple of things I tried without success:

        int count = 0;
        for (boolean[] i : landscape){
            count += i.length;


        }
        return count;

and

       int count = 0;

        for (int i = 0; i < landscape.length; i++) {

                if (landscape[i] != null){
                    count ++;

            }

        }
        return count;

The number of rows and columns depends on what the user selects. There is at least 5. How can I do this?

+3
source share
6 answers

Let's start with some definitions:

Suppose a rectangular array 3 x 4has 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

, , () . ( , , .)

- , 3 4, , .


, .

+19

, 2D - . , 2D- 5 4 , , 5 , 4 .

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("");
}

:

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

, java ( ): 2D-, ? , ( 0: 0, 1, 2, 3, 4, 1: 0, 1 ..), 2D , , , , , ; .

+3

return landscape[0].length? , landscape[0] , , .

, ( ) landscape[rows][cols], landscape.length, cols - . .

+2

:

                  0 1 0
                  1 0 1
                  1 0 0
                  0 0 1

2D- java 4 .
:

int[][] matrix=new int[4][3];

java 2D- , . , .

, matrix[1] 1. matrix[1] - . . , matrix[1].length . , . 0 .

, , .

In fact,

int row = matrix.length;    //4 for above example
int col = matrix[0].length; //3 for above example

I hope I can answer your question.

+1
source

Your code should work.

    int count = 0;

    for (int i = 0; i < landscape.length; i++) {

            if (landscape[i] != null){
                count ++;

        }

    }
    return count;

Can you put your code in the way you populate your 2D array?

Here is a project that I did that counted the 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++;
        }
    }
0
source

Stephen, your code has a slight error in accordance with the general agreement. According to the standard convention, the following should indicate the correct values ​​for the rows and columns:

int rows = matrix.length;
int cols = matrix[0].length;

In addition, this assumes that you already have an array populated and checking the number of rows and columns at the end.

0
source

All Articles