

class Oct10 {

	public static void main(String[] args) {

		// A 2-dimensional array is an array of arrays.

		int numRows = 3;
		int rowLength = 5;

		int[][] matrix = new int[numRows][rowLength]; 

		printMatrix(matrix);

		int[] row1 = { 1, 2, 3, 4, 5};
		int[] row2 = { 1, 2, 3, 4, 5};
		int[] row3 = { 1, 2, 3, 4, 5};

		matrix[0] = row1;
		matrix[1] = row2;
		matrix[2] = row3;

		printMatrix(matrix);

		// set the last element of the 2nd row to 7
		matrix[1][4] = 7;

		printMatrix(matrix);
	}

	static void printMatrix(int[][] matrix) {
		
		for(int[] row : matrix) {
			for(int elm : row) {
				System.out.print(elm + " ");
			}
			System.out.println();
		}
		System.out.println();
		
		/*  
		// Another way to print the elements of a 2D array
		for(int i = 0; i < matrix.length; i++) {
			int[] row = matrix[i];

			for(int j = 0; j < row.length; j++) {
				System.out.print(row[j] + " ");
			}
			System.out.println();
		}
		System.out.println();

		// Yet, another way to print the elements of a 2D array
		for(int i = 0; i < matrix.length; i++) {
			for(int j = 0; j < matrix[i].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
		}
		System.out.println();
		*/
	}
}
