
public class TwoDimensionalArrays {

	public static void main(String[] args) {
		
		//4 rows and 6 columns
		int[][] table = new int[4][6];

		//set value of row 1 column 2
		table[1][2] = 45;
		table[3][4]++;
		//Can't do this, its an array: table[1]++;
		System.out.println(table[1][2]);
		
		//set all the value in row 2 to 15
		for(int col = 0; col < table[2].length; col++) {
			//System.out.printf("%3d ", table[2][col]);
			table[2][col] = 15;
		}
		
		//set all the values in column 3 to 12
		for(int row = 0; row < table.length; row++) {
			table[row][3] = 12;
		}
		
		printTable(table);
		System.out.println();
		
		fillTable(table, 100);
		
		printTable(table);
		System.out.println();
		
		//ragged array - rows are different lengths
		int[][] anotherArray = { {1,2}, {5, 6, 7}, {8, 9, 2, 3}  };
		/*
		anotherArray = new int[10][];
		for(int i = 0; i < 10; i++) {
			anotherArray[i] = new int[i+1];
		}
		*/
		printTable(anotherArray);
		
		fillTable(anotherArray, 50);
		printTable(anotherArray);
		
	} //end main
	
	//print all the value in the 2D array
	public static void printTable(int[][] arr2D) {
		//for each row
		//arr2D.length is the number of rows
		for(int row = 0; row <  arr2D.length; row++) {
			//for each column in this row, arr2D[row[
			for(int col = 0; col < arr2D[row].length; col++) {
				System.out.printf("%3d ", arr2D[row][col]);
			}
			//print end of line
			System.out.println();
		}
		System.out.println("--------------------------------");
	}
	
	//fill the 2D array with random values 0..max
	public static void fillTable(int[][] array, int max) {
		//arr2D.length is the number of rows
		for(int row = 0; row <  array.length; row++) {
			//for each column in this row, arr2D[row[
			for(int col = 0; col < array[row].length; col++) {
				array[row][col] = (int)(Math.random()*max);
			}
		}
			
	}

}
