0

How many elements does this array contain

A [5][5]

27th Mar 2019, 12:25 PM
Vanshaj Nathani
Vanshaj Nathani - avatar
12 ответов
+ 2
Vanshaj Nathani I made a code to make it more clear https://code.sololearn.com/cATThMCAQzok/?ref=app The length is not 6 when bound is 5. It's still 5. (0 1 2 3 4 5) //length 5
27th Mar 2019, 1:09 PM
Seniru
Seniru - avatar
+ 3
25 = 5*5 - elements
27th Mar 2019, 12:42 PM
Kuri
Kuri - avatar
+ 1
25 as Nagendra said. Explanation: Basically this is a 2d matrix - with 5 rows and 5 columns So if we illustrated our matrix (array) it will look like this 0 1 2 3 4 0 ✖✖✖✖✖ 1 ✖✖✖✖✖ 2 ✖✖✖✖✖ 3 ✖✖✖✖✖ 4 ✖✖✖✖✖ ✖ = 1 item. Therefore it is obvious that we have 25 elements. Extra fact: If we have a 3D array, the total number of elements we have is the product of x, y and z dimensions. eg: new int[5][5][5] //will contain 125 ints
27th Mar 2019, 12:57 PM
Seniru
Seniru - avatar
+ 1
But in a[2][2] I have 9 elements
27th Mar 2019, 12:59 PM
Vanshaj Nathani
Vanshaj Nathani - avatar
0
But array starts from 0 So 6*6 right??
27th Mar 2019, 12:52 PM
Vanshaj Nathani
Vanshaj Nathani - avatar
0
So this should have 4 a[2][2]
27th Mar 2019, 12:56 PM
Vanshaj Nathani
Vanshaj Nathani - avatar
0
int [][] a={{1,2,3},{2,3,4},{3,4,5}}; System.out.print(a[2][2]);
27th Mar 2019, 1:01 PM
Vanshaj Nathani
Vanshaj Nathani - avatar
0
Output came 5
27th Mar 2019, 1:01 PM
Vanshaj Nathani
Vanshaj Nathani - avatar
0
Oh ok Got it!! Thanks
27th Mar 2019, 1:06 PM
Vanshaj Nathani
Vanshaj Nathani - avatar