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.
Trang 1Chapter 8
Arrays and the ArrayList Class
Multi-Dimensional Arrays
Trang 2Contents
I Two-Dimensional Arrays
II Arrays with Three or More Dimensions
Trang 3I Two-Dimensional Arrays
1 Creating a Two-Dimensional Array
2 Initializing a Two-Dimensional Array
3 The length Field in a Two-Dimensional Array
4 Displaying All the Elements of a
Two-Dimensional Arrays
5 Summing All the Elements of a
Two-Dimensional Array
6 Summing the Rows of a Two-Dimensional
Array
Trang 4I Two-Dimensional Arrays
7 Summing the Columns of a Two-Dimensional Array
8 Passing Two-Dimensional Arrays to Methods
9 Ragged Arrays
Trang 5I.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
Row 0 Row 1 Row 2
Column
0 Column 1 Column 2 Column 3
Trang 6I.1 Creating a Two-Dimensional
Array
To declare a two-dimensional array:
double[][] scores = new double[3][4];
Two set of brackets
Two size declarators
The first one is for the number of rows: 3
The second one is for the number of columns: 4
Two sets of brackets
indicate
a two-dimensional
array
Number of rows
Number of columns
Trang 7I.1 Creating a Two-Dimensional
Array
In the scores array:
The elements in row 0:
The elements in row 1:
The elements in row 2:
Trang 8I.1 Creating a Two-Dimensional
Array
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]
Row 0 Row 1 Row 2
Column
address
The scores
variable
Trang 9I.1 Creating a Two-Dimensional
Array
Programs that process two-dimensional arrays
can do 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();
}
}
Trang 10I.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} };
Trang 11I.3 The length Field in a
Two-Dimensional Array
A two-dimensional array can be considered as an array of one-dimensional arrays
int[][] numbers = new int[3][4];
addres s addres s addres s
numbers[1][1] numbers[1][2] numbers[1][3] numbers[1][0]
numbers[0][1] numbers[0][2] numbers[0][3] numbers[0][0]
numbers[2][1] numbers[2][2] numbers[2][3] numbers[2][0]
numbers[
0]
numbers[
1]
numbers[
2]
address
The numbers
variable numbers.length 3
numbers[0].length 4
numbers[1].length 4
numbers[2].length 4
Trang 12I.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]);
}
Trang 13I.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 0
//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);
Trang 14I.5 Summing the Rows of a
Two-Dimensional Array
Calculate the sum of each row in a
two-dimensional 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 0
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);
}
Trang 15I.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 0
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);
}
Trang 16I.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 two-dimensional array.
Trang 17I.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 individual rows
ragged[0] = new int[3]; //row 0 has 3 elements
ragged[1] = new int[4]; //row 1 has 4 elements
ragged[2] = new int[5]; //row 2 has 5 elements
ragged[3] = new int[6]; //row 3 has 6 elements
Trang 18I.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]
//
}
}
Trang 19II 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