Java Array multidimensional Arrays sum all elements

Description

Java Array multidimensional Arrays sum all elements

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    int[][] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

    System.out.println(Arrays.deepToString(matrix));
    int total = 0; 
    for (int row = 0; row < matrix.length; row++) { 
       for (int column = 0; column < matrix[row].length; column++) { 
        total += matrix[row][column]; // ww w  . j a  v  a2  s.c  o m
      } 
    } 
    
    System.out.println(total);
  }
}



PreviousNext

Related