Java Array multidimensional Arrays pass to methods

Description

Java Array multidimensional Arrays pass to methods

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[][] m = new int[3][4];
    for (int i = 0; i < m.length; i++)
      for (int j = 0; j < m[i].length; j++)
        m[i][j] = (int)(Math.random()*10);

    System.out.println(Arrays.deepToString(m));
    /*from  w  ww . ja  va  2 s  .co m*/
    // Display result
    System.out.println("\nSum of all elements is " + sum(m));
  }
  
  public static int sum(int[][] m) {
    int total = 0;
    for (int row = 0; row < m.length; row++) {
      for (int column = 0; column < m[row].length; column++) {
        total += m[row][column];
      }
    }

    return total;
  }
}



PreviousNext

Related