Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Arrays
Chapter 10 Objectives
After you have read and studied this
chapter, you should be able to
Manipulate a collection of data values,
using an array.
Declare and use an array of primitive data
types in writing a program.
Declare and use an array of objects in
writing a program.
Fig. 10.1
Sample array: Monthly rainfall averages
and their variation from the annual
average.
Fig. 10.2
An array of 12 double values.
Fig. 10.3
An array of 12 double values after all
12 are assigned values.
Fig. 10.4
An array of Person objects after the
array is created.
Fig. 10.5
The person array with one Person
object added to it.
File: Ch10ProcessPersonArray.java
*/
import javax.swing.*;
class Ch10ProcessPersonArray {
public static void main (String[] args) {
Person[]
person; //declare the person array
person = new Person[5];
//and then create it
inpStr =JOptionPane.showInputDialog(null,
"Enter gender:");
gender = inpStr.charAt(0);
Fig. 10.6
An array of Person objects with two
Person variables.
Fig. 10.7
Approach 1 deletion: Setting a reference to
null. The array length is 4.
Fig. 10.8
Approach 2 deletion: Replace the removed
element with the last element in the array.
The array length is 4.
Fig. 10.9A
Passing an array to a method means we
are passing a reference to an array.
Fig. 10.9B
Passing an array to a method means we
are passing a reference to an array.
Fig. 10.10A
Passing an array to a method means we
are passing a reference to an array.
Fig. 10.10B
Passing an array to a method means we
are passing a reference to an array.
Fig. 10.11A
The effect of creating a local array and not
returning it.
Fig. 10.11B
The effect of creating a local array and not
returning it.
Fig. 10.12A
The effect of creating a local array and not
returning it.
Fig. 10.12B
The effect of creating a local array and not
returning it.
or
double payScaleTable[][];
Fig. 10.15
Examples of information represented as
tables.
Fig. 10.16
Accessing an element of a twodimensional array.
is a shorthand for
payScaleTable = new double [4][ ];
payScaleTable[0] = new double [5];
payScaleTable[1] = new double [5];
payScaleTable[2] = new double [5];
and so on.
TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fig. 10.17A
Executing the statements on the left in
sequence will create the array of
arrays shown on the right.
Fig. 10.17B