Assumed Knowledge:
Learning Outcomes:
  • Creating and operating on an array of objects.

Author: Gaurav Gupta

Array of objects

Class under consideration

We will use the following class for this discussion:

1
2
3
4
5
6
7
8
9
10
11
12
public class Rectangle {
	public double width, height;

	public Rectangle(double w, double h) {
		width = w;
		height = h;
	}

	public String toString() {
		return width + " by " + height;
	}
}

Creating an array of objects

STEP 1 - Instantiate the array

1
2
3
4
5
public class Client {
	public static void main(String[] args) {
		Rectangle[] blocks = new Rectangle[5];
	}
}

You can go through each item of the array and display it.

1
2
3
for(int i=0; i < blocks.length; i++) {
	System.out.println(blocks[i]);
}

You’ll get the following output:

1
2
3
4
5
null
null
null
null
null

The memory diagram for the current state of the array is

Each item of the array is a Rectangle reference, and initialised to the default value (which, for objects, is null).

At this point, none of the items of the array refer to an instance of type Rectangle. This is checked using instanceof operation. So the following would display Does not refer to Rectangle instance.

1
2
3
4
5
6
if(blocks[0] instanceof Rectangle) {
	System.out.println("Refers to Rectangle instance");
}
else {
	System.out.println("Does not refer to Rectangle instance");
}

Any attempt to access an instance variable or instance method on any of the items of the array will throw a NullPointerException.

1
2
blocks[0].width = 5; // NullPointerException
String str = blocks[0].toString(); // NullPointerException

Hence…

STEP 2 - Instantiating each object

1
2
3
4
for(int i=0; i < blocks.length; i++) {
	blocks[i] = new Rectangle(i+1, i*2); // instantiate item at index i
	System.out.println(blocks[i]); // display it
}

This time, you will get the following output:

1
2
3
4
5
1.0 by 0.0
2.0 by 2.0
3.0 by 4.0
4.0 by 6.0
5.0 by 8.0

Exercise: Creating (filtered) deep copy of an object array

Consider the array source populated as:

1
2
3
4
5
6
Rectangle[] source = new Rectangle[20];
for(int i=0; i < source.length; i++) {
	double randWidth = 1 + rand.nextInt(5);
	double randHeight = 1 + rand.nextInt(5);
	source[i] = new Rectangle(randWidth, randHeight);
}

Create a second array containing Rectangles that have an area of 10 or more.

STEP 1: Count the number of rectangles with area of 10 or more

1
2
3
4
5
6
int count = 0;
for(int i=0; i < source.length; i++) {
	if(source[i].area() >= 10) {
		count++;
	}
}

STEP 2: Create an array of required size

1
Rectangle[] bigRectangles = new Rectangle[count];

STEP 3: Populate the array

1
2
3
4
5
6
7
int destIndex = 0;
for(int i=0; i < source.length; i++) {
	if(source[i].area() >= 10) {
		bigRectangles[destIndex] = source[i];
		destIndex++;
	}
}

Given an array data of Rectangle objects, create an array wider with those rectangles whose width is more than their height

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int count = 0;
for(int i=0; i < data.length; i++) {
	if(data[i].width > data[i].height) {
		count++;
	}
}
Rectangle[] wider = new Rectangle[count];
int destIndex = 0;
for(int i=0; i < data.length; i++) {
	if(data[i].width > data[i].height) {
		wider[destIndex] = data[i];
		destIndex++;
	}
}

You can also access an instance variable or instance method on any of the items of the array.

1
2
blocks[0].width = 5; // change the width of the first item to 5
int h = blocks[3].height; // store height of fourth item into h

The complete code for Rectangle and ArrayOfObjects is provided in ArrayOfObjects.java.

Relevant MQ Video