Java Native array length

I have a 2D binary array in Java. It is basically a value table. I want to know how many rows it has

It declares elsewhere (and distribution) as follows:

double[][] table;

Then pass it to a function

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

}

In my function, I want to know the length of each dimension without passing them as parameters I can do this for columns, but I don't know how to do it for rows

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

What do I do?

I can say

int rows = table.length;

Why not give the line x cols?

Solution

In Java, a 2D array is just an array

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

int rows = array.length;

This also means that each row in this array can have a different number of elements (that is, each row can have a different number of columns)

int columnsInFirstRow = array[0].length;

This will only give you the number of columns in the first row, but the second row may have more or fewer columns

You can specify that your method only takes rectangular arrays and assume that the number of columns per row is the same as the first row But in this case, I will wrap the 2D array in some matrix classes (you may need to write it)

This array is called jagged array

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>