Java 多维数组中array.length 和array[0].length 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24850618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:12:48  来源:igfitidea点击:

Difference between array.length and array[0].length in a multi-dimensional array?

javaarrays

提问by Isaac Adni

When I create a new multi-dimensional array (in this case an array of objects)

当我创建一个新的多维数组(在这种情况下是一个对象数组)

public Block[][] block = new Block[50][70];

What is the difference between:

有什么区别:

block.length

and

block[0].length

and then what is

然后是什么

block[1].length

采纳答案by gexicide

What do you expect? You have a multidimensional array. Thus, there is more than one dimension. Each dimension has a length. With block.lengthyou get the length of the first one (i.e. 50), with block[x].length, you get the length of the second one (i.e., 70).

你能指望什么?你有一个维数组。因此,存在不止一个维度。每个维度都有一个长度。随着block.length你得到的第一个(即长度50),有block[x].length,你得到的第二个(即长度70)。

Since you allocated your array like this, all block[x].lengthwill be equal, no matter what you choose for x. However, you could have an array where the nested arrays have different lengths, then block[0].lengthmight not be equal to block[1].length.

既然你这样分配了你的数组block[x].length,那么无论你选择什么,一切都将是平等的x。但是,您可能有一个数组,其中嵌套数组的长度不同,然后block[0].length可能不等于block[1].length

回答by Shahar

block.lengthis the number of rows. block[0].lengthis the number of columns in row 0. block[1].lengthshould have the same value as block[0].lengthsince it has the same number of columns as the first row.

block.length是行数。block[0].length是第 0 行中的列数。block[1].length应该具有相同的值,block[0].length因为它与第一行的列数相同。

回答by Bohemian

If you think of Block[][]as being rows and columns of Block, and each row being a Block[]of columns, and an array of rows would be a Block[][], then:

如果您认为Block[][]是 的行和列Block,每行是列的 a ,而行Block[]的数组是 a Block[][],那么:

block.length // the number of rows
block[0].length // the number of columns on row 0
block[1].length // the number of columns on row 1
// etc

回答by micronyks

here you have two dimensional array. it has 50 rows and 70 columns. it means...

在这里你有二维数组。它有 50 行和 70 列。它的意思是...

about block array... block array will look like (row & column wise)

关于块数组...块数组看起来像(行和列)

block=   0...1...2...3...4...SO..ON...69
         1
         2           50
         3
         4
         5
         SO..ON
         ..
         ..
         49

Now you are having tabular format of block array. now lets say I want to get value of block[2][3]... it means 2th row and 3rd column which is 50 as shown above.

现在您拥有块数组的表格格式。现在可以说我想获得 block[2][3] 的值......这意味着第 2 行和第 3 列是 50,如上所示。

block.length total rows of Block array.

block.length 块数组的总行数。

block[0] is referring to 0th row of Block array and so block[0].length gives you columns associated with 0th row which 70 so ans would be 70 (Note:total count)....

block[0] 指的是 Block 数组的第 0 行,因此 block[0].length 为您提供与第 0 行相关联的列,其中 70 为 70(注意:总计数)...

and so on....

等等....