An Animated Introduction To Computer Science
With Processing And Java

Thomas C. O'Connell


DRAFT 2024


Copyright © 2016, 2024 Thomas C. O'Connell, All Rights Reserved

Chapter 4 : Lists

4.1 Introduction

A single house is nice and all, but we need to think big. Let’s create a neighborhood of a hundred identical houses. Using what we know so far, we would have to define 100 object references in the main Processing object and add a call to each of the House’s display methods in the main Processing object’s draw method. There must be a better way.

Rather than having to define a new identifier for each object reference, we are going to tell Java that we need a whole list of object references. We do this by creating an ArrayList object. As with any other object we create, we need to define an object reference for the ArrayList. The ArrayList is a little different from the objects we have created so far. Because an ArrayList keeps track of a list of object references, we need to tell Java what type of object references the ArrayList will maintain. To define an ArrayList of Houses, we define an object reference like so:

ArrayList<House> houseList

Placing the class name House inside the angled brackets indicates that the object references in the list will refer to House objects. This is referred to as a type parameter – we tell Java what type of thing is being stored in the list.

Again, like any other object that is part of the main Processing object, we need to construct the ArrayList object in the setup method with the statement:

houseList = new ArrayList<House>();

It is extremely important to realize that this constructs an ArrayList object, but it does not construct the objects to which the members of the list will refer. You will undoubtedly make the mistake of thinking that by constructing the list, you are also constructing the objects to be referenced in the list – try to remember that you are not.

Before we get too carried away with creating a sprawling neighborhood, let’s try adding just four House objects to our list. The ArrayList includes an add method, which add object references to the list, but we have to construct the House objects before we add references to them to the list. Since we do not need to keep separate references to the House objects, however, we can simply put our new statements directly in the parentheses of the add method, like so:

houseList.add( new House(200, 200, 245, 244, width/2, height/2) );

Let’s be clear about what this line of code is doing. The new-statement constructs a House object, passing the given parameters to the House constructor. Java then takes a reference to this newly constructed House object and passes it to the add method of the houseList object, which we constructed previously in setup. The add method adds this object reference to the houseList ArrayList.

To construct four houses, we can include four such statements in the setup method, passing in different values for the initial x location in the parameters for the House constructor:

  houseList.add( new House(200, 200, 245, 244, width/2-220, height/2) );

  houseList.add( new House(200, 200, 245, 244, width/2, height/2) );

  houseList.add( new House(200, 200, 245, 244, width/2+220, height/2) );

  houseList.add( new House(200, 200, 245, 244, width/2+440, height/2) );

Now that we have four references to House objects in our list, we would like to tell draw to display each of the Houses. It would be nice if we could say something like for each House referenced by our list, display the House. Java, in fact, provides us with a way to say exactly that, using something called a for-loop, although with typically restrictive syntax.

Like the if statement, the for-loop is followed by parens that specify how the for-loop should operate. In this case, we could say something like this:

  for (House theHouse:houseList) {

    theHouse.display();

  } // end for

This for-loop works by repeating the block of code inside the brackets for each House object referenced in the ArrayList houseList. Inside this block of code, theHouse is used to refer to objects referenced in the ArrayList; if we want each of the Houses in the list to run some method, we use theHouse as the object reference, followed by the dot, the method name, and the parameter list. The complete code for the main Processing object is shown in Figure 4.1.1. Notice that the checking of mousePressed is inside the for-loop. This enables us to call the windstorm method for each House object in the list if the mouse is pressed. Figure 4.1.2 shows the roofs of all four houses blowing away when the mouse is pressed.

image not found


Figure 4.1.1: The main Processing class with multiple Houses.

Your browser does not support the canvas tag.



Figure 4.1.2: Four houses losing their roofs in unison when the mouse is pressed.

Let’s look at the for-statement more closely. In the parentheses, we are declaring a new local variable called theHouse. As with any variable that we create, we need to tell Java what the type of the variable is. In this case, the type of the variable is House. That is why the first thing we write inside the parentheses of the for-loop is House theHouse. Following the variable declaration, we put a colon followed by the name of the list that we are going to be looping over. In this case, the name of the list is houseList. This tells Java that we are doing to execute the statements in the body of the loop for each House object referenced by the list houseList. In the body of the loop, we use our newly declared variable theHouse to refer to the different House objects referenced by the list. In this particular example, we want to call the display method for each House object in the list. When calling a method, we need to provide a reference to the object containing the method. In this case, that reference will be the variable theHouse, which will reference a different object each time the body of the loop is executed. In other words, the first time the body is executed, theHouse will refer to the first object referenced by the list; the second time the body is executed, theHouse will refer to the second object referenced by the list, and so on, until the body of the loop has been executed for each House referred to by the list. After the body has been executed for each House referenced in the list, the loop will terminate and theHouse will be gone, that is, theHouse variable will not exist after the loop terminates.

Back To Top