+ 1

What will happen if you use length function on 2-D Array.. ???

When I tried this.....but....!

31st Jan 2018, 2:21 PM
Mihir
Mihir - avatar
4 Respostas
+ 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.
31st Jan 2018, 2:28 PM
Dev
Dev - avatar
0
It will provide u d length of an array The size of row n column
1st Feb 2018, 11:06 AM
priya gupta
priya gupta - avatar
0
I tried doing...."int[][] arr=new int[5][4];"then.... "System.out.println(arr.length);"......and the output was 5!?.....how come...
1st Feb 2018, 4:31 PM
Mihir
Mihir - avatar
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.
21st Feb 2018, 6:08 PM
Harshit Upadhyay
Harshit Upadhyay - avatar