In Chapter 6, "Arrays," arrays of primitive type elements were created. You can also create arrays of objects. For example, the following statement declares and creates an array of ten Circle objects:
Circle[] circleArray = new Circle[ 10 ];
To initialize the circleArray , you can use a for loop like this one:
for ( int i = ; i < circleArray.length; i++) { circleArray[i] = new Circle(); }
An array of objects is actually an array of reference variables . So invoking circleArray[1].getArea() involves two levels of referencing, as shown in Figure 7.20. circleArray references the entire array. circleArray[1] references a Circle object.
Note
When an array of objects is created using the new operator, each element is a reference variable with a default value of null . |
Listing 7.7 gives an example that demonstrates how to use an array of objects. The program summarizes the areas of an array of circles. The program creates circleArray , an array composed of ten Circle3 objects; it then initializes circle radii with random values, and displays the total area of the circles in the array. The output of a sample run of the program is shown in Figure 7.21.
1 public class TotalArea { 2 /** Main method */ 3 public static void main(String[] args) { 4 // Declare circleArray 5 Circle3[] circleArray; 6 7 // Create circleArray 8 circleArray = ; createCircleArray() 9 10 // Print circleArray and total areas of the circles 11 printCircleArray(circleArray); 12 } 13 14 /** Create an array of Circle objects */ 15 public static Circle3[] createCircleArray() { 16 Circle3[] circleArray = new Circle3[ 10 ]; 17 18 for ( int i = ; i < circleArray.length; i++) { 19 circleArray[i] = new Circle3(Math.random() * 100 ); 20 } 21 22 // Return Circle array 23 return circleArray; 24 } 25 26 /** Print an array of circles and their total area */ 27 public static void printCircleArray 28 (Circle3[] circleArray) { 29 System.out.println( "Radius\t\t\t\t" + "Area" ); 30 for ( int i = ; i < circleArray.length; i++) { 31 System.out.print(circleArray[i].getRadius() + "\t\t" + 32 circleArray[i].getArea() + '\n' ); 33 } 34 35 System.out.println( " “ “ “ “ “ “ “ “ “ “ “ “ “ ”" ); 36 37 // Compute and display the result 38 System.out.println( "The total areas of circles is \t" + 39 sum(circleArray) ); 40 } 41 42 /** Add circle areas */ 43 public static double sum(Circle3[] circleArray) { 44 // Initialize sum 45 double sum = ; 46 47 // Add areas to sum 48 for ( int i = ; i < circleArray.length; i++) 49 sum += circleArray[i].getArea(); 50 51 return sum; 52 } 53 } |
The program invokes createCircleArray() (line 8) to create an array of ten Circle3 objects. Several Circle classes were introduced in this chapter. This example uses the Circle3 class introduced in §7.8, "Data Field Encapsulation."
The circle radii are randomly generated using the Math.random() method (line 19). The createCircleArray method returns an array of Circle3 objects (line 23). The array is passed to the printCircleArray method, which displays the radii of the total area of the circles.
The sum of the areas of the circle is computed using the sum method (line 39), which takes the array of Circle3 objects as the argument and returns a double value for the total area.