Java native arrays length

I have a two-dimensional array of doubles in Java, which is basically a table of values, and I want to find out how many rows it has ...

It is declared elsewhere (and allocated) as follows:

double[][] table;

then passed to the function ...

private void doSomething(double[][] table)
{

}

In my function, I want to know the length of each dimension without having to pass them as arguments. I can do this for the number of columns, but don't know how to do this for rows ...

int cols = table[0].length;
int rows = ?;

How to do it?

Can I just say ...

int rows = table.length;

Why won't this give an x ​​cols string?

+3
source share
6 answers

In Java, a 2D array is nothing more than an array of arrays.

This means that you can easily get the number of lines like this:

int rows = array.length;

, (.. ).

int columnsInFirstRow = array[0].length;

, .

, , , . 2D- Matrix ( , , ).

Jagged Array.

+18

. .length, , [].length, .

, "", , .length , .

+2

. , Java .

table.length , ( , ).

table[0].length . , NullPointerException ( , new double[rows][cols]).

+1

, -, ,

int rows = table.length;

?

. , ... - 2D- - ( ):

double[][] foo = { double[] = {...}, double[] = {...}, ... }

, foo "" , , , length, , .

double [] [] " ([]) (double [])".

+1

, . table.length

0

Alternatively, you can combine your 2d array into a Matrix or Grid class. Internally, you can represent it as a 1d array and calculate the offsets for the row / column coordinates.

0
source

Source: https://habr.com/ru/post/1703803/


All Articles