Chapter 8

¡Supera tus tareas y exámenes ahora con Quizwiz!

Multidimensional Arrays

-An array in which each element is another array -A n'th dimensional array is an array of n-1 dimensional arrays; so a 3D array is just an array of 2D arrays, and a 4D array is just an array of 3D arrays

Array Initializer for 2d Arrays

-Can also use an array initializer to declare, create, and initialize a two-dimensional array ex. int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; OR (equivalent) int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;

Two-Dimensional Array

-Can use a two-dimensional array to store a matrix or a table -Syntax: elementType[][] arrayRefVar; or elementType arrayRefVar[][]; // Allowed, but not preferred or dataType [][] refVar; refVar = new dataType[10][10]; or combine in one statement dataType [][] refVar = new dataType[10][10]; -Two subscripts are used in a two-dimensional array, one for the row and the other for the column -A two-dimensional array is actually an array in which each element is a one-dimensional array ex. To assign the value 7 to a specific element at row 2 and column 1, you can use the following syntax: matrix[2][1] = 7; -Using .length will return the number of elements in the array

Ragged Arrays

-Each row in a two-dimensional array is itself an array; thus, the rows can have different lengths; an array of this kind is known as a ragged array ex. int[][] triangleArray = { {1,2,3,4,5}, {2,3,4,5}, {3,4,5}, {4,5}, {5} }; -If you don't know the values in a ragged array in advance, but do know the sizes—say, the same as before—you can create a ragged array using the following syntax: ex. int[][] triangleArray = new int[5][]; triangleArray[0] = new int[5]; triangleArray[1] = new int[4]; triangleArray[2] = new int[3]; triangleArray[3] = new int[2]; triangleArray[4] = new int[1]; -Note: the syntax new int[5][] for creating an array requires the first index to be specified; the syntax new int[][] would be wrong

Processing 2d Arrays

-Initializing arrays with input values The following loop initializes the array with user input values: java.util.Scanner input = new Scanner(System.in); System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextInt(); } } -Initializing arrays with random values. The following loop initializes the array with random values between 0 and 99: for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = (int)(Math.random() * 100); } } -Printing arrays; to print a two-dimensional array, you have to print each element in the array using a loop like the following: for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column] + " "); } System.out.println(); } -Summing all elements. Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: int total = 0; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { total += matrix[row][column]; } } -Summing elements by column. For each column, use a variable named total to store its sum. Add each element in the column to total using a loop like this: for (int column = 0; column < matrix[0].length; column++) { int total = 0; for (int row = 0; row < matrix.length; row++) total += matrix[row][column]; System.out.println("Sum for column " + column + " is " + total); } -Which row has the largest sum? Use variables maxRow and indexOfMaxRow to track the largest sum and index of the row. For each row, compute its sum and update maxRow andindexOfMaxRow if the new sum is greater. int maxRow = 0; int indexOfMaxRow = 0; // Get sum of the first row in maxRow for (int column = 0; column < matrix[0].length; column++) { maxRow += matrix[0][column]; } for (int row = 1; row < matrix.length; row++) { int totalOfThisRow = 0; for (int column = 0; column < matrix[row].length; column++) totalOfThisRow += matrix[row][column]; if (totalOfThisRow > maxRow) { maxRow = totalOfThisRow; indexOfMaxRow = row; } } System.out.println("Row " + indexOfMaxRow + " has the maximum sum of " + maxRow); -Random shuffling. Shuffling the elements in a one-dimensional array was introduced in Section 7.2.6. How do you shuffle all the elements in a two-dimensional array? To accomplish this, for each element matrix[i][j], randomly generate indices i1 and j1 and swap matrix[i][j] with matrix[i1][j1], as follows: for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { int i1 = (int)(Math.random() * matrix.length); int j1 = (int)(Math.random() * matrix[i].length); // Swap matrix[i][j] with matrix[i1][j1] int temp = matrix[i][j]; matrix[i][j] = matrix[i1][j1]; matrix[i1][j1] = temp; } }

Passing a 2d Array to a Method

-When passing a two-dimensional array to a method, the reference of the array is passed to the method -Method header: (As arg) public static returnType methodName(dataType[][] arrayName) -Method header: (As return type) public static arrayDataType[][] methodName(parameters)


Conjuntos de estudio relacionados

Intro to webpage creation final exam

View Set

A+ 220-1101 Exam Acronyms Quiz - Part 1

View Set

Psych of Personality Final- Unit 11

View Set

高職龍騰英文 B1 (B版) L5 Three Strange Guests 單字&片語

View Set

N360 - Theory - Evolve questions

View Set

Chapter 7: Driving Safely and Studying for your Permit

View Set