public class TwoDimensionalArrays { public static void main(String[] args) { // Define a fixed-size two-dimensional array of Strings with // second-dimension (sub) arrays of all the same length String words[][] = new String[5][5]; words[0][4] = "chair"; words[3][1] = "computer"; System.out.println(words[0][4]); System.out.println(words[3][1]); System.out.println("Size of two-dimensional 'words' array: " + words.length + "\n"); // Define a fixed-size two-dimensional array of Strings with // second-dimension (sub) arrays of varying lengths String[][] animals = new String[2][]; animals[0] = new String[6]; animals[1] = new String[3]; animals[0][0] = "LAND ANIMALS"; animals[0][1] = "cat"; animals[0][2] = "dog"; animals[0][3] = "elephant"; animals[0][4] = "giraffe"; animals[0][5] = "bear"; animals[1][0] = "WATER ANIMALS"; animals[1][1] = "shark"; animals[1][2] = "dolphin"; for (int i = 0; i < animals[0].length; i++) System.out.println(animals[0][i]); System.out.println(); for (int i = 0; i < animals[1].length; i++) System.out.println(animals[1][i]); System.out.println(); System.out.println("Size of 1st dimension 'Animals' array: " + animals.length); System.out.println("Size of 2nd dimension 'Land Animals' array: " + animals[0].length); System.out.println("Size of 2nd dimension 'Water Animals' array: " + animals[1].length); System.out.println(); // Define a fixed-size two-dimensional array of characters char letters[][] = new char[4][6]; // Fill all 24 array elements with letters int spot = 65; for (int i = 0; i < letters.length; i++) for (int j = 0; j < letters[0].length; j++) letters[i][j] = (char) spot++; // Display all 24 letters in the array for (int i = 0; i < letters.length; i++) { for (int j = 0; j < letters[0].length; j++) System.out.print(letters[i][j] + " "); System.out.println(); } } }