The List type
6.7 The List type
Dealing with collections of objects is important both in Greenfoot programming and in pro-
gramming in general. Several of the Greenfoot methods return collections of objects as their result, usually in the form of a list. The type of the returned object then is the List type from the
Concept:
A collection is a
java.util package.
kind of object that can hold many other objects.
Side Note: Interfaces Concept:
The List type is a little different from other object types we have seen: It is not a class, but an interface. Interfaces are a Java construct that provides an abstraction over different possible
A List is an exam- implementing classes. The details are not important to us right now—it is sufficient to know ple of a collection.
that we can deal with the List type in similar ways as with other types: We can look it up in the
Some methods
from the Greenfoot Java Library Documentation, and we can call the existing methods on the object. We cannot, API return List
however, create objects directly of type List . We will come back to this issue later.
objects.
Exercise 6.19 Look up java.util.List in the Java Library Documentation. What are the names of the methods used to add an object to the list, remove an object from the list, and to find out how many objects are currently in the list?
Exercise 6.20 What is the proper name of this type, as given at the top of the documentation?
When we looked at the getObjects method in the previous section, we noticed that it returns an object of type java.util.List . Thus, in order to store this object, we need to declare a variable of this type. We will do this in our applyForces method.
6.8 The for-each loop
The List type, however, is different from other types we have seen before. The documentation shows at the top
Interface List<E>
Apart from the word interface in place of class, we notice another new notation: the < E > after the type name.
Concept:
Formally, this is called a generic type. This means that the type List needs an additional type specified as a parameter. This second type specifies the type of the elements held within the list.
A generic type is a type that
For example, if we are dealing with a list of strings, we would specify the type as
receives a second type name as
List<String> a parameter.
If instead we are dealing with a list of actors, we can write
List<Actor>
In each case, the type within the angle brackets ( <> ) is the type of some other known kind of object. In our case, we expect a list of bodies, so our variable declaration will read:
List<Body> bodies
Now we can assign the list which we retrieve from the getObjects method to this variable:
List<Body> bodies = getWorld().getObjects(Body. class );
After executing this line, our variable bodies holds a list of all bodies that currently exist in the world (see also Code 6.5). (Remember that you must also add an import statement for java.util.List at the top of your class.)
Code 6.5 Getting a list of all
private void applyForces() bodies in space
{ List<Body> bodies = getWorld().getObjects(Body. class ); ...