public class MdArrayExamples { public static void print2DArray(int[][] a) { for (int i = 0; i < a.length; ++i) { for (int j = 0; j < a[i].length; ++j) System.out.print(a[i][j] + " "); System.out.println(); } } public static void print2DArray2(int[][] a) { for (int[] row : a) { for (int col : row) System.out.print(col + " "); System.out.println(); } } public static int sumArray(int[] a) { int sum = 0; for (int i = 0; i < a.length; ++i) sum += a[i]; return sum; } public static int maxRow(int[][] a) { int maxRow = sumArray(a[0]); int maxRowIndex = 0; for (int i = 1; i < a.length; ++i) if (sumArray(a[i]) > maxRow) { maxRow = sumArray(a[i]); maxRowIndex = i; } return maxRowIndex; } public static int sumColumn(int[][] a, int colIndex) { int sum = 0; for (int i = 0; i < a.length; ++i) sum += a[i][colIndex]; return sum; } public static int maxColumn(int[][] a) { int maxCol = sumColumn(a, 0); int maxColIndex = 0; for (int i = 1; i < a[0].length; ++i) if (sumColumn(a, i) > maxCol) { maxCol = sumColumn(a, i); maxColIndex = i; } return maxColIndex; } public static void main(String[] args) { int[] a = { 1, 2, 3, 4 }; System.out.println(a.length); int[][] b = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } }; System.out.println(b.length + ", " + b[0].length); int[][][] c = { { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } }, { { 4, 5, 6, 7 }, { 5, 6, 7, 8 }, { 6, 7, 8, 9 } } }; System.out.println(c.length + ", " + c[0].length + ", " + c[0][0].length); print2DArray(b); print2DArray2(b); System.out.println(maxRow(b)); System.out.println(maxColumn(b)); } }