+ 1
What will happen if you use length function on 2-D Array.. ???
When I tried this.....but....!
4 ответов
+ 8
Talking about Java, the length function always gives the number of elements in the array. So is the case with 2D arrays, as the array contains 2 elements (the two arrays are treated as 2 different elements), the number returned is 2.
Take this as an example,
int[][] arr = {{2, 4, 8}, {1, 3, 5}};
System.out.print(arr.length);
This would give 2 as output, just because there are two elements in it. If it had been arr[0].length then the result is 3.
0
It will provide u d length of an array
The size of row n column
0
I tried doing...."int[][] arr=new int[5][4];"then.... "System.out.println(arr.length);"......and the output was 5!?.....how come...
0
@Mihir it is because length variable of an array stores the value passed in the 1st Square Brackets (forgot their formal name). So whenever you'll access the length variable of any multidimensional array it will return the value that was passes in the 1st Square Brackets while the creation of the array.