
public class TwoDimensionalArrays {

	public static void main(String[] args) {
		//2-D Arrays
		//create a 2D array with 4 rows and 6 columns
		int[][] table = new int[4][6];
		
		//modify individual elements
		table[1][2] = 10;
		table[3][4]++;
		
		System.out.println(table[1][2]);
		
		printTable(table);
		
		//set values for row 2 to 15
		for(int col = 0; col < table[2].length; col++) {
			table[2][col] = 15;
		}

		printTable(table);
		
		//set value for col 3 to 12
		for(int row = 0; row < table.length; row++) {
			table[row][3] = 12;
		}
		
		printTable(table);
		
		fillTable(table, 100);
		
		printTable(table);
		
		//ragged array
		int[][] anotherArray = { {1, 2, 3}, 
								 {4, 5}, 
								 {7, 8, 9, 0} };
		printTable(anotherArray);
		
		fillTable(anotherArray, 10);
		printTable(anotherArray);
	}
	
	public static void printTable(int[][] array) {
		//for each row in the array
		for(int row = 0; row < array.length; row++) {
			//print out all of the columns
			for(int col = 0; col < array[row].length; col++) {
				System.out.printf("%3d ", array[row][col]);
			}
			System.out.println();  //end of row
		}
		//end of the table
		System.out.println("---------------------------------------");
	}
	
	//fill the table with random value between 0 and max
	public static void fillTable(int[][] array, int max) {
		//for each row in the array
		for(int row = 0; row < array.length; row++) {
			//print out all of the columns
			for(int col = 0; col < array[row].length; col++) {
				array[row][col] = (int)(Math.random()*max);
			}
		}
		
	}

}
