Output
1 import java.util.Scanner;
2
3 public class PassTwoDimensionalArray {
4 public static void main(String[] args) {
5 int[][] m = getArray();
6
7
8 System.out.println("\nSum of all elements is " + sum(m));
9 }
10
11 public static int[][] getArray() {
12
13 Scanner input = new Scanner(System.in);
14
15
16 int[][] m = new int[3][4];
17 System.out.println("Enter " + m.length + " rows and "
18 + m[0].length + " columns: ");
19 for (int i = 0; i < m.length; i++)
20 for (int j = 0; j < m[i].length; j++)
21 m[i][j] = input.nextInt();
22
23 return m;
24 }
25
26 public static int sum(int[][] m) {
27 int total = 0;
28 for (int row = 0; row < m.length; row++) {
29 for (int column = 0; column < m[row].length; column++) {
30 total += m[row][column];
31 }
32 }
33
34 return total;
35 }
36 }