Game Development Reference
In-Depth Information
java.lang.Object
> java.util.AbstractCollection<E>
> java.util.AbstractList<E>
> java.util. ArrayList<E>
This class is a member of the Java Collections Framework , as you might have
surmised, as a List, as well as an Array, both contain collections of data, much like a
data structure (or a data store) does, only in a simpler format. An ArrayList<E> class
can “ implement ” or support the following Java Interfaces: Serializable, Cloneable,
Iterable<E>, Collection<E>, List<E>, and RandomAccess. We will be using the
List<E> or, in our case, List<Actor> Java interface, which we will be looking at in the
next section on List<E> when we will learn about Java Interfaces .
Essentially the ArrayList class (and object) creates a resizable Array implementa-
tion of the List<E> interface. An ArrayList object thus implements all optional List op-
erations, and permits all types of List elements, including null . In addition to imple-
menting the List<E> Java interface, this class also provides method calls, including a
.removeAll() , .addAll() , and .clear() , which we will be using in our class, to manipu-
late both the List content as well as the size of the ArrayList object that is used intern-
ally to store the List of Actors (for List<Actor>) or Images (for List<Image>) used.
Each ArrayList object instance has a capacity. The capacity is the size of the Array
used to store the elements (objects) in the List<E> implementation: in our case, a
List<Actor>. The capacity will always be at least as large as the List size. As elements
(Actor objects) are added to an ArrayList, its capacity will grow automatically, which
makes it perfect for our CastingDirector class, as we can make levels of the game more
and more complex that is, it can utilize more Actor objects in the ArrayList<Actor> of
List<Actor>.
It is important to note that the List<E> implementation is not synchronized (cap-
able of running on multiple threads simultaneously). If you need to have multiple
threads access an ArrayList instance concurrently (at the same exact time), and at least
one of these multiple threads modifies your List<Actor> structure, it must be synchron-
ized externally (manually, using your code). We are going to call the CastingDirector
class specifically when an enemy is killed, or a projectile is shot, or a treasure is found
(collected) and will not have it being continually called on a pulse.
A structural modification of an ArrayList object is an operation that adds or re-
moves one or more elements; merely setting the value of an element (Actor) in the Ar-
rayList would not be considered to be structural modification.
Search WWH ::




Custom Search