0

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

4 Answers 4

7

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

"Each dimension has a length" - not really, each subarray has a length, and they can be of different lengths. They're "jagged arrays".
@kviiri: That sentence was not about Java arrays in general but arrays when used as multi-dimensional arrays with new X[y][z].
I'd still clarify that, as the question is specifically about Java arrays.
2

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....

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.