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
| 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
| 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
|
This is because none of the items of the array, each a Rectangle
object, were instantiated.
The memory diagram for the current state of the array is

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].getWidth() > data[i].getHeight()) {
count++;
}
}
Rectangle[] wider = new Rectangle[count];
int destIndex = 0;
for(int i=0; i < data.length; i++) {
if(data[i].getWidth() > data[i].getHeight()) {
wider[destIndex] = data[i];
destIndex++;
}
}
|