class Array2D { /* * Creates and initializes an int-array of dimension (m, n) */ static int[][] make(int m, int n) { int[][] a = new int[m][n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) a[i][j] = i * j; return a; } // end of make() /* * Prints a 2D int-array */ static void show(int[][] a) { for (int i = 0; i < a.length; i++) { System.out.print("{ "); for (int j = 0; j < a[0].length; j++) System.out.print(a[i][j] + " "); System.out.println("}"); } } // end of show() /* * Computes the sum of all the elements in a 2D int-array */ static int sum(int[][] a) { int sum = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) sum += a[i][j]; return sum; } // end of sum() /* * Driver to test the above methods */ public static void main(String[] args) { int[][] a; a = make(4, 5); System.out.println(); show(a); System.out.println(); System.out.println("Sum of a is " + sum(a)); } // end of main() } // end of Array2D class