import java.util.Scanner;

class Oct20 {
	public static void main(String[] args) {

		// declare 3 arrays of integers
		int[] row1 = new int[3];
		int[] row2 = new int[3];
		int[] row3 = new int[3];

		// declare 2D array that contains the 3 rows
		int[][] matrix = new int[3][];    	// note: int[#rows][]
		matrix[0] = row1;
		matrix[1] = row2;
		matrix[2] = row3;
		
		// alternates
		// int[][] matrix = { row1, row2, row3 };
		// int[][] matrix = new int[3][3];	// int[#rows][#cols]

		// print only the first row in matrix 
		System.out.println("First row...");
		int[] firstRow = matrix[0];
		for(int i = 0; i < firstRow.length; i++) {
			System.out.printf("%d ",firstRow[i]);
		}
		System.out.println();
		System.out.println();

		// print all of the rows in matrix
		System.out.println("All rows...");
		for(int i = 0; i < matrix.length; i++) {
			int[] row = matrix[i];
			for(int j = 0; j < row.length; j++) {
				System.out.printf("%d ", row[j]);
			}
			System.out.println();
		}
		System.out.println();

		// print all rows alt - replace row with matrix[i]
		System.out.println("Better - All rows...");
		for(int i = 0; i < matrix.length; i++) {
			for(int j = 0; j < matrix[i].length; j++) {
				System.out.printf("%d ", matrix[i][j]);
			}
			System.out.println();
		}
		System.out.println();

		// print first row of matrix with for-each loop
		System.out.println("First row with for-each...");
		for(int elm : matrix[0]) {
			System.out.printf("%d ", elm);
		}
		System.out.println();
		System.out.println();

		// print all rows in matrix with for-each loops
		System.out.println("All rows with for-each...");
		for(int[] row : matrix) {
			for(int elm : row) {
				System.out.printf("%d ", elm);
			}
			System.out.println();
		}
		System.out.println();

		// Set all of the elements in matrix to 5
		for(int i = 0; i < matrix.length; i++) {
			for(int j = 0; j < matrix[i].length; j++) {
				matrix[i][j] = 5;
			}
		}

		// print all rows in matrix with for-each loops
		System.out.println("All rows...");
		for(int[] row : matrix) {
			for(int elm : row) {
				System.out.printf("%d ", elm);
			}
			System.out.println();
		}
		System.out.println();

		// compute sum
		int sum = 0;
		for(int[] row : matrix) {
			for(int elm : row) {
				sum = sum + elm;
			}
		}
		System.out.printf("Sum: %d\n", sum);
	}
}
