+ 1

explain a[0].length in 2nd loop please tell me how its working

int a[][] = { { 1, 3, 4 }, { 2, 3, 6 }, { 7, 6, 5 } }; int sum = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { if (a[i][j] % 2 == 0) break; sum += a[i][j]; } } System.out.println("sum = " + sum); } }

29th Dec 2021, 9:30 AM
Vinay
Vinay - avatar
3 ответов
+ 1
From the program it looks like you are trying to add the odd numbers in the given 2D array. int arr[][] = {{1,3,4},{2,3,6},{7,6,5}} Here arr[0] = {1,3,4} And arr[0].length = 3 For the program to work properly you should use arr[i].length because you might have inner arrays with different lengths.
29th Dec 2021, 9:40 AM
Avinesh
Avinesh - avatar
+ 1
yes I got it answer of my question a[0].length simply means Zeroth indexs element length a = { 1, 3, 4 }, { 2, 3, 6 }, { 7, 6, 5 } So at zeroth index there are 3 elements 1,,3,4 so length is 3 Or a.length is the number of rows. a[0].length is the number of columns.
29th Dec 2021, 9:52 AM
Vinay
Vinay - avatar