Chapter 8 Chapter 8 Arrays and the ArrayList Class Multi Dimensional Arrays 2 Contents I Two Dimensional Arrays II Arrays with Three or More Dimensions 3 I Two Dimensional Arrays 1 Creating a Two Dime.
Chapter Arrays and the ArrayList Class Multi-Dimensional Arrays Contents I Two-Dimensional Arrays II Arrays with Three or More Dimensions I Two-Dimensional Arrays Creating a Two-Dimensional Array Initializing a Two-Dimensional Array The length Field in a Two-Dimensional Array Displaying All the Elements of a TwoDimensional Arrays Summing All the Elements of a TwoDimensional Array Summing the Rows of a Two-Dimensional Array I Two-Dimensional Arrays Summing the Columns of a Two-Dimensional Array Passing Two-Dimensional Arrays to Methods Ragged Arrays I.1 Creating a Two-Dimensional Array Two-dimensional array, which are sometimes called 2D arrays, is an array of arrays It has rows and columns of elements Column Column Column Column Row Row Row I.1 Creating a Two-Dimensional Array To declare a two-dimensional array: double[][] scores = new double[3][4]; Two sets of brackets indicate a two-dimensional array Number of rows Number of columns Two set of brackets Two size declarators The first one is for the number of rows: The second one is for the number of columns: I.1 Creating a Two-Dimensional Array In the scores array: The elements in row 0: scores[0][0] scores[0][2] scores[0][1] scores[0][3] The elements in row 1: scores[1][0] scores[1][2] scores[1][1] scores[1][3] The elements in row 2: scores[2][0] scores[2][2] scores[2][1] scores[2][3] I.1 Creating a Two-Dimensional Array The scores variable address Column Row Row Row Column Column Column scores[0][0] scores[0][1] scores[0][2] scores[0][3] scores[1][0] scores[1][1] scores[1][2] scores[1][3] scores[2][0] scores[2][1] scores[2][2] scores[2][3] I.1 Creating a Two-Dimensional Array Programs that process two-dimensional arrays can so with nested loops final int ROWS = 3; final int COLS = 4; double[][] scores = new double[ROWS][COLS]; for(int row = 0; row < ROWS; row++) { for(int col = 0; col < COLS; col ++) { System.out.print(“Enter a score: “); scores[row][col] = keyboard.nextDouble(); } } I.2 Initializing a Two-Dimensional Array Enclose each row's initialization list in its own set of braces: int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Row Row Row 10 I.3 The length Field in a TwoDimensional Array A two-dimensional array can be considered as an array of one-dimensional arrays int[][] numbers = new int[3][4]; The numbers variable address numbers[ 0] numbers[ 1] numbers[ 2] numbers.length numbers[0].length numbers[1].length numbers[2].length addres s addres s addres s 4 numbers[0][0] numbers[0][1] numbers[0][2] numbers[0][3] numbers[1][0] numbers[1][1] numbers[1][2] numbers[1][3] numbers[2][0] numbers[2][1] numbers[2][2] numbers[2][3] 11 I.3 Displaying All the Elements of a Two-Dimensional Array int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; //Display all the elements in the array for(int row = 0; row < 3; row++) { for(int col = 0; col < 4; col++) System.out.println(numbers[row][col]); } //A better approach to display all elements in the array for(int row = 0; row < numbers.length; row++) { for(int col = 0; col < numbers[row].length; col++) System.out.println(numbers[row][col]); } 12 I.4 Summing All the Elements of a Two-Dimensional Array int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; int acc = 0; //Accumulator, set to //Sum the array elements for(int row = 0; row < numbers.length; row++) { for(int col = 0; col < numbers[row].length; col++) acc += numbers[row][col]; } //Display the sum System.out.println(“The total is ” + acc); 13 I.5 Summing the Rows of a TwoDimensional Array Calculate the sum of each row in a twodimensional array: int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; int acc; //Accumulator for(int row = 0; row < numbers.length; row++) { //Set the accumulator to acc = 0; //Sum a row for(int col = 0; col < numbers[row].length; col++) acc += numbers[row][col]; //Display the sum System.out.println(“The total of row is ” + acc); } 14 I.6 Summing the Column of a Two-Dimensional Array The outer loop controls the column subscript and the inner loop controls the row subscript int[][] numbers = { {1, 2, 3, 4}, {4, 5, 6, 8}, {9, 10, 11, 12} }; int acc; //Accumulator for(int col = 0; col < numbers[0].length; col++) { //Set the accumulator to acc = 0; //Sum a column for(int row = 0; row < numbers.length; row++) acc += numbers[row][col]; //Display the sum System.out.println(“The total of column is ” + acc); } 15 I.7 Passing Two-Dimensional Arrays to Methods When a two-dimensional array is passed to a method, the parameter must be declared as a reference to a two-dimensional array public static void showArray(int[][] array) The method's parameter, array, is declared as a reference to a twodimensional array 16 I.8 Ragged Arrays When the rows of a two-dimensional array are of different lengths, the array is known as a ragged array Creating a ragged array: //Creating a two-dimensional array, the array has four //rows, but the rows have not yet been created int[][] ragged = new int[4][]; //Creat the ragged[0] = ragged[1] = ragged[2] = ragged[3] = individual rows new int[3]; //row new int[4]; //row new int[5]; //row new int[6]; //row has has has has elements elements elements elements 17 I.8 Ragged Arrays Processing elements of a ragged array: for(int row = 0; row < ragged.length; row++) { for(int col = 0; col < ragged[row].length; col++) { // Processing the element ragged[row][col] // } } 18 II Arrays with Three or More Dimensions Java does not limit the number of dimensions that an array may have A three-dimensional array declaration: double[][][] array = new double[3][5][7]; This array can be though of as three sets of five rows Each row contains seven elements 19 ... //Display the sum System.out.println(? ?The total of row is ” + acc); } 14 I.6 Summing the Column of a Two-Dimensional Array The outer loop controls the column subscript and the inner loop controls the. .. //Display the sum System.out.println(? ?The total is ” + acc); 13 I.5 Summing the Rows of a TwoDimensional Array Calculate the sum of each row in a twodimensional array: int[][] numbers = { {1, 2, 3, ... Displaying All the Elements of a TwoDimensional Arrays Summing All the Elements of a TwoDimensional Array Summing the Rows of a Two-Dimensional Array I Two-Dimensional Arrays Summing the Columns