Home  >  Article  >  Java  >  How to implement summation of array elements in Java

How to implement summation of array elements in Java

王林
王林forward
2023-05-08 20:40:061089browse

Sum of elements in an array

public class T02 {
    public static void main(String[] args) {
        int[][]arr=new int[][]{{1,2,3,4,5},{1,2,3,5},{8,9,7}};
        int sum=0;
        for(int i=0;i< arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
               sum=arr[i][j]+sum;
            }
        }
        System.out.println("sum="+sum);
    }
}
//和为50

The above is the detailed content of How to implement summation of array elements in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete