+ 1
Do you have an idea on how to get the sum of elements of this multidimensional array?
int[ ][] myArray = {{2, 3, 5, 7} ,{2, 3, 5, 7}} ;
2 Answers
+ 4
Just use two for loops:
int[ ][] myArray = {{2, 3, 5, 7} ,{2, 3, 5, 7}};
int sum=0;
for(int i=0;i<2;i++){
for(int j=0;j<4;j++){
sum+=myArray[i][j];
}
}
System.out.print(sum);
+ 1
for unknown length of an array this code wiil help you.
int a[][] = {{2,3,5,7},{2,3,5,7}};
int sum=0;
for(int i=0; i<a.length;i++){
for(int j=0; j<a[i].length; j++){
sum+=a[i][j];
}
}
System.out.println(sum);