Reputation: 841
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
Upvotes: 0
Views: 24811
Reputation: 55453
here you have two dimensional array. it has 50 rows and 70 columns. it means...
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.length total rows of Block array.
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)....
and so on....
Upvotes: 2
Reputation: 425458
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.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
Upvotes: 7
Reputation: 1677
block.length
is the number of rows. block[0].length
is the number of columns in row 0. block[1].length
should have the same value as block[0].length
since it has the same number of columns as the first row.
Upvotes: 1
Reputation: 40128
What do you expect? You have a multi dimensional array. Thus, there is more than one dimension. Each dimension has a length. With block.length
you 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
).
Since you allocated your array like this, all block[x].length
will 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].length
might not be equal to block[1].length
.
Upvotes: 2