0
How many elements does this array contain
A [5][5]
9 odpowiedzi
+ 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
+ 3
25 = 5*5 - elements
+ 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
+ 1
But in a[2][2] I have 9 elements
0
But array starts from 0
So 6*6 right??
0
So this should have 4 a[2][2]
0
int [][] a={{1,2,3},{2,3,4},{3,4,5}};
System.out.print(a[2][2]);
0
Output came 5
0
Oh ok
Got it!!
Thanks