7.4 Arrays of Objects
We said earlier that the elements of an array could be of any type. This includes the possibility that elements can be objects. To illustrate this,
consider a class that could be used to represent complex numbers. We could begin to define such a class by writing
class Complex
{
float re;
float im;
}
To create an array of Complex objects, we could proceed as follows. In our setup method, say, we could write
Complex[] points = new Complex [100];
This would create an array of 100 values (points [0], points [1], ... , points [99]) of type reference to Complex. As with the declaration of any array of references, each of these would be initialized to null. To actually create 100 Complex objects, we could use the following loop.
for(int i = 0; i < points.length; i++)
points[i] = new Complex();
Assuming that the Complex class has a toString method, we could print the value of the fifth object in the array by writing
println(points[4]);
Objects can be very useful in situations that call for a number of arrays of the same size. As an example, suppose that we are collecting data on
a group of children. We might have an array name to store their names, another array age to store their ages in years, a third array height to store their heights in metres, and a fourth array sex to store their sex as a character (M or F). These could be declared as follows:
String[] name = new String [STUDY_SIZE];
int[] age = new int[STUDY_SIZE];
double[] height = new double [STUDY_SIZE];
char[] sex = new char [STUDY_SIZE];
Arrays of the same length, carrying data on different aspects of the same problem, are sometimes called parallel arrays. Rather than using four
parallel arrays for our study of child development, we could use a single array of objects.
Example 1
We could begin to define a class that could be used for a study of children's development as follows:
class Child
{
String name; //family, given
int age; //in years
float height; //in metres
char sex; //M or F
}
Now, to create a structure to store data on the children, we could write
Child[] patient = new Child[STUDY_SIZE];
The data for the first child in the study would now be called patient[0]. The age of this child would be patient[0].age.
|
|
There are a number of advantages to using an array of objects rather than multiple arrays of data.
- A method that processes data about one object can be given a single object parameter rather than many item parameters.
- If we modify the program later to include different kinds of data, we need only change the fields of the object class; no parameters need to be added to our methods.
Example 2
Consider the Car class from our last assignment. Suppose we wanted the create 100 cars in random locations, with random directions and random colours. Without worrying about them crashing into each other we could write a program like this:
Car [] cars = new Car[100];
void setup()
{
size(800,700);
background(255);
for (int i = 0; i < cars.length; i++)
{
cars[i] = new Car((int)random(700),(int)random(600));
cars[i].colour = color((int)random(255),(int)random(255),(int)random(255));
cars[i].dir = (int)random(0, 4) - 2;
}
}
void draw()
{
background(255);
for (int i = 0; i < cars.length; i++)
cars[i].move();
}
|
|
Suppose we do want to check all those cars for crashing. We could cut down the traffic a little and just make 20 cars (so they don't all crash pretty well at once). We also want to wait for a bit before they start crashing, so we can see the crashes in action. We could use the statement:
if (frameCount > 100) so that the draw method will get executed 100 times before we start checking for crashing. We would modify the array declaraton at the beginning of the program in example 2 to:
Car [] cars = new Car[20];
and our draw method as follows:
void draw()
{
background(255);
for (int i = 0; i < cars.length; i++)
cars[i].move();
if (frameCount > 100)
for (int j = 0; j < cars.length - 1; j++)
for (int i = j+1; i < cars.length; i++)
cars[j].collidingWith(cars[i]);
}
|
|
Exercises 7.4
- For the points array discussed in this section, write a fragment that would set all the values of the array to represent the complex number 1 + i. You may assume the existence of a constructor with header public Complex(double re, double im).
-
- For the child development study discussed in this section, write a method that could be added to the Child class that prints the data (name, age, height, and sex) for a child on two lines with the name on the first line and the other data on the second line. Print the height in centimetres, rounded to the nearest centimetre.
- Write a fragment that uses this method to print the data on all the children.
- Write a method that would produce the same output as the method shown in part (a) if we had chosen to use parallel arrays rather than an array of objects to represent our data.
- Assuming that a Fraction class has been defined as shown, write a fragment that could be used to create an array of 100 Fraction objects representing the fractions

class Fraction
{
int num;
int den;
Fraction (int n, int d)
{
num = n;
den = d;
}
}
| |