Java本机数组的长度 [英] Java native array lengths

查看:104
本文介绍了Java本机数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中双打的二维数组基本上是值的表,我想找出它有多少行有...

I have a 2D 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) like this:

double[][] table;

然后传递给函数...

then passed to a function...

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

}

在我的功能,我想知道每个维度的长度,而不必绕它们作为参数传递。我能做到这一点的列数,但不知道如何做到这一点的行...

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

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

我该怎么办呢?

我只能说......

Can I just say...

int rows = table.length;

为什么会不给列x栏?

Why would that not give rows x cols?

推荐答案

在Java中的二维数组只不过是数组的数组了。

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

这意味着你可以轻松获得行这样的数字:

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

int rows = array.length;

这也意味着,每个行这种阵列中的可以的具有不同量元素(即每行可具有不同数量的列)。

This also means that each row in such an array can have a different amount of elements (i.e. each row can have a varying 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 could have more or less columns than that.

您可以指定自己的方法只需要矩形阵列和假设的每一行具有相同数量比第一个列。但是,在这种情况下,我会换一些Matrix类二维阵列(也就是说你可能写)。

You could specify that your method only takes rectangular arrays and assume that each row has the same number of columns than the first one. But in that case I'd wrap the 2D-array in some Matrix class (that you might have to write).

这类型的数组被称为铁血阵列

这篇关于Java本机数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆