Game Development Reference
In-Depth Information
The List<E> Public Interface: A List Collection of
Java Objects
The List<E> public interface is also a member of the Java Collections Framework.
The Java public interface List<E> extends the Collections<E> public interface, which
extends the Iterable<T> public interface. Thus, the super interface to sub interface hier-
archy would look something like this following List<E> Java interface hierarchy:
Interface Iterable<T>
> Interface Collection<E>
> Interface List<E>
A List<E> is an ordered Collection<E> and could also be thought of as a se-
quence of objects. In our case, the List<Actor> will be an ordered sequence of Actor
objects. A user of the List interface has precise control over where in the List each ele-
ment (in our case, Actor object) is inserted. The user can access elements using an in-
teger index , that is, the position in the List, using a parenthesis after the name of the
List. You can also search for elements in a List.
For instance, in the Actor.java class, we have the following line of code we de-
clared at the top of the class:
protected List<Image> imageStates = new ArrayList<>();
To access the first Actor class imageState Image object List sprite, we will use the
following Java statement:
imageStates.get(0);
Unlike Set objects, which we will be learning about in the next section of the
chapter, your List<E> interface conformant ArrayList objects will typically allow du-
plicate elements. An example of this for a game application might include projectiles
(say, bullets) if you have coded the game to allow the Enemy object to shoot at the Ba-
gel object. We will, of course, try to keep duplicate elements in our game to a minim-
um for optimization purposes, but it is nice to have this capability in the List<Actor>
implementation if we need to have duplicate elements in a game scene.
The List<E> interface provides four methods for positional (indexed) access to List
elements (objects) using the integer index in the method call. These include the
.get(int index) method, to get an object from the List; the .remove(int
Search WWH ::




Custom Search