Do you have an idea on how to get the sum of elements of this multidimensional array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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}} ;

23rd Mar 2016, 6:53 AM
michael angelo
michael angelo - avatar
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);
17th May 2016, 2:47 PM
James Flanders
+ 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);
4th Jul 2016, 4:06 PM
Rishabh Ranjan
Rishabh Ranjan - avatar