Escolar Documentos
Profissional Documentos
Cultura Documentos
Chapter 9 Arrays
Chapter Objectives
Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of array index out of bounds Become familiar with the restrictions on array processing
Java Programming: From Problem Analysis to Program Design, 3e 2
Array
Definition: structured data type with a fixed number of elements Elements of an array are also called components of the array Every element is of the same type Elements are accessed using their relative positions in the array
Java Programming: From Problem Analysis to Program Design, 3e 4
One-Dimensional Arrays
Arrays
Array num: int[] num = new int[5];
Array List
10
11
12
13
14
15
16
This statement creates the array numList of 10 components and initializes each component to 0
Java Programming: From Problem Analysis to Program Design, 3e 17
These statements store 5, 10, 15, and 20, respectively, in the first four components of numList You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say numOfElement It is a common practice for a program to keep track of the number of filled elements in an array
Java Programming: From Problem Analysis to Program Design, 3e 18
Arrays (continued)
Some operations on arrays:
Initialize Input data Output stored data Find largest/smallest/sum/average of elements
double[] sales = new double[10]; int index; double largestSale, sum, average;
20
21
22
23
24
25
26
27
If index < 0 or index > arraySize: ArrayIndexOutOfBoundsException exception is thrown Base address: memory location of first component in array
Java Programming: From Problem Analysis to Program Design, 3e 28
30
31
32
35
36
37
38
39
Parallel Arrays
Arrays are parallel if corresponding components hold related information
40
Arrays of Objects
Can use arrays to manipulate objects Example: create array named array1 with N objects of type T T[] array1 = new T[N] Can instantiate array1 as follows: for(int j=0; j <array1.length; j++) array1[j] = new T();
Java Programming: From Problem Analysis to Program Design, 3e 41
42
43
44
45
46
47
48
49
foreach loop
The syntax to use this for loop to process the elements of an array is:
for (dataType identifier : arrayName) statements
identifier is a variable and the data type of identifier is the same as the data type of the array components
52
The for statement in Line 2 is read: for each num in list The identifier num is initialized to list[0 In the next iteration, the value of num is list[1], and so on
for (double num : numList) { if (max < num) max = num; }
Java Programming: From Problem Analysis to Program Design, 3e 53
Two-Dimensional Arrays
54
55
56
57
58
is 15, the number of columns in the first row matrix[1].length gives the number of columns in the second row, which in this case is 15, and so on
Java Programming: From Problem Analysis to Program Design, 3e 60
61
62
63
64
65
66
Print
for (row = 0; row < matrix.length; row++) { for (col = 0; col < matrix[row].length; col++)
System.out.printf("%7d", matrix[row][col]);
System.out.println(); }
Java Programming: From Problem Analysis to Program Design, 3e 67
Sum by Row
for (row = 0; row < matrix.length; row++) { sum = 0; for (col = 0; col < matrix[row].length; col++) sum = sum + matrix[row][col];
System.out.println("Sum of row " + (row + 1) + " = "+ sum);
}
Java Programming: From Problem Analysis to Program Design, 3e 68
69
}
Java Programming: From Problem Analysis to Program Design, 3e 70
71
Multidimensional Arrays
Can define three-dimensional arrays or n-dimensional array (n can be any number) Syntax to declare and instantiate array dataType[][][] arrayName = new
dataType[intExp1][intExp2][intExpn];
73
74
Value in appropriate index incremented using methods and depending on character read from text
Java Programming: From Problem Analysis to Program Design, 3e 75
Chapter Summary
Arrays
Definition Uses
Different Arrays
One-dimensional Two-dimensional Multidimensional (n-dimensional) Arrays of objects Parallel arrays
Java Programming: From Problem Analysis to Program Design, 3e 76