Array of Objects

Paul Ngugi - Jun 3 - - Dev Community

An array can hold objects as well as primitive type values. Single-Dimensional Arrays, described how to create arrays of primitive type elements. 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 circleArray, you can use a for loop like this one:

for (int i = 0; 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 below. circleArray references the entire array; circleArray[1] references a Circle object. When an array of objects is created using the new operator, each element in the array is a reference variable with a default value of null.

Image description

The program below 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 five Circle objects; it then initializes circle radii with random values and displays the total area of the circles in the array.

package demo;

public class TotalArea {

    public static void main(String[] args) {
        // Declare circleArray
        CircleWithPrivateDataFields[] circleArray;

        // Create circleArray
        circleArray = createCircleArray();

        // Print circleArray and total areas of the circles
        printCircleArray(circleArray);
    }

    /** Create an array of Circle objects */
    public static CircleWithPrivateDataFields[] createCircleArray() {
        CircleWithPrivateDataFields[] circleArray = new CircleWithPrivateDataFields[5];

        for(int i = 0; i < circleArray.length; i++) {
            circleArray[i] = new CircleWithPrivateDataFields(Math.random() * 100);
        }

        // Return Circle array
        return circleArray;
    }

    /** Print an array of circles and their total area */
    public static void printCircleArray(CircleWithPrivateDataFields[] circleArray) {
        System.out.printf("%-30s%-15s\n", "Radius", "Area");
        for(int i = 0; i < circleArray.length; i++) {
            System.out.printf("%-30f%-15f\n", circleArray[i].getRadius(), circleArray[i].getArea());
        }

        System.out.println("------------------------------------------");

        // Compute and display the result
        System.out.printf("%-30s%-15f\n", "The total area of circles is", sum(circleArray) );
    }

    /** Add circle areas */
    public static double sum(CircleWithPrivateDataFields[] circleArray) {
        // Initialize sum
        double sum = 0;

        // Add areas to sum
        for(int i = 0; i < circleArray.length; i++)
            sum += circleArray[i].getArea();

        return sum;
    }

}

Enter fullscreen mode Exit fullscreen mode

Radius Area
70.577708 15649.941866
44.152266 6124.291736
24.867853 1942.792644
5.680718 101.380949
36.734246 4239.280350
—————————————————————————————————————————————-
The total area of circles is 28056.687544

The program invokes createCircleArray() (line 10) to create an array of five circle objects.

The circle radii are randomly generated using the Math.random() method (line 21). The createCircleArray method returns an array of CircleWithPrivateDataFields objects (line 25). The array is passed to the printCircleArray method, which displays the radius and area of each circle and the total area of the circles.

The sum of the circle areas is computed by invoking the sum method (line 38), which takes the array of CircleWithPrivateDataFields objects as the argument and returns a double value for the total area.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .