28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

For instance, in the Actor.java class, we have the following line of code we declared at the top of the class:<br />

protected List imageStates = new ArrayList();<br />

To access the first Actor class imageState Image object List sprite, we will use the following <strong>Java</strong> statement:<br />

imageStates.get(0);<br />

Unlike Set objects, which we will be learning about in the next section of the chapter, your List interface<br />

conformant ArrayList objects will typically allow duplicate elements. An example of this for a game application might<br />

include projectiles (say, bullets) if you have coded the game to allow the Enemy object to shoot at the Bagel object.<br />

We will, of course, try to keep duplicate elements in our game to a minimum for optimization purposes, but it is nice to<br />

have this capability in the List implementation if we need to have duplicate elements in a game scene.<br />

The List interface provides four methods for positional (indexed) access to List elements (objects) using<br />

the integer index in the method call. These include the .get(int index) method, to get an object from the List; the<br />

.remove(int index) method, to remove an object from the List; the .set(int index, E element) method, which<br />

will replace an object within the List; and a .listIterator() method, which returns a ListIterator object from<br />

the List. A ListIterator object allows you to perform an operation (add, remove, set/replace) on more than one List<br />

element at a time, in case you might be wondering what a ListIterator is utilized for.<br />

The List interface provides this special Iterator implementation, called a ListIterator, which is a sub<br />

interface of the Iterator super interface, to allow multiple element insertion and replacement. A ListIterator<br />

also provides bidirectional List access, in addition to the normal operations that the Iterator interface provides.<br />

The .listIterator() method that we discussed earlier was provided to obtain a ListIterator object that starts at a<br />

specified position in the list. So using an imageStates List ArrayList object, a imageStates.listIterator()<br />

method call would produce a ListIteration object containing an iteration over an entire imageStates ArrayList<br />

object. This would give us imageStates(0), the starting List element, as well as the remainder of this List in an<br />

ArrayList construct, which would be referenced as imageStates(1), imageStates(2), and the last one would<br />

be referenced as imageStates(8). <strong>Java</strong> List class use () parens to reference List objects, whereas <strong>Java</strong> Array classes use<br />

the [] square brackets. A <strong>Java</strong> List is “dynamic” (luckily, we have discussed static versus dynamic); that is, it’s openended,<br />

whereas a <strong>Java</strong> Array is “static,” or fixed, which means that its length needs to be defined when it is created.<br />

Objects that implement List start their numbering schema using a zero, just like all <strong>Java</strong> Array objects. This is<br />

not out of the ordinary, as most <strong>Java</strong> programming constructs will also start counting at zero instead of using one. It is<br />

important to note from an optimization standpoint that iterating over the elements in List is typically preferable<br />

(more optimal) to indexing through it by number, for instance using a for loop, which is probably why support for this<br />

ListIterator interface is a part of the List interface specification, and therefore is a part of the ArrayList class, which<br />

has no choice but to implement the List interface specification because it uses the <strong>Java</strong> implements keyword.<br />

The List interface also provides three methods allowing access to the List using a specified object. These<br />

include .indexOf(Object object), .contains(Object object), and .remove(Object object). Again, looking at it<br />

strictly from a performance standpoint, these methods should be used with caution, because having to compare an<br />

“input” object to every object inside of the List is going to take way more memory and CPU cycles than simply using<br />

the index for the object in the List. After all, that is what an index is for! In many implementations this will perform<br />

a costly “linear” object search, starting at ListElement[0] and going through the entire list comparing objects. If your<br />

object is at the “head” of this List, this would not be costly at all. One the other hand, if your object is at the end of a<br />

List containing a great many object elements, you may well observe a performance hit using these “object oriented”<br />

methods. Well, all methods are object oriented, so, let’s cleverly call these methods “object parameterized” instead!<br />

The List interface also provides two methods to efficiently read or remove multiple List elements at an<br />

arbitrary point within the List. The .removeRange(int fromIndex, int toIndex) removes a range of List elements,<br />

and the .subList(int fromIndex, int toIndex) returns a view of the portion of the List between the specified<br />

fromIndex and the toIndex. The fromIndex is included in the returned sub-list, however, the toIndex is not included.<br />

www.it-ebooks.info<br />

211

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!