Game Development Reference
In-Depth Information
index) method, to remove an object from the List; the .set(int index, E
element) method, which will replace an object within the List; and a .listIter-
ator() method, which returns a ListIterator object from the List. A ListIterat-
or object allows you to perform an operation (add, remove, set/replace) on more than
one List element at a time, in case you might be wondering what a ListIterator is util-
ized for.
The List interface provides this special Iterator<E> implementation, called a
ListIterator<E> , which is a sub interface of the Iterator<E> super interface, to allow
multiple element insertion and replacement. A ListIterator<E> also provides bidirec-
tional List access, in addition to the normal operations that the Iterator<E> interface
provides.
The .listIterator() method that we discussed earlier was provided to obtain a
ListIterator object that starts at a specified position in the list. So using an imageStates
List<Image> ArrayList object, a imageStates.listIterator() method call
would produce a ListIteration object containing an iteration over an entire imageStates
ArrayList<Image> object. This would give us imageStates(0), the starting List element,
as well as the remainder of this List<Image> in an ArrayList<Image> construct, which
would be referenced as imageStates(1), imageStates(2), and the last one would be ref-
erenced as imageStates(8). Java List class use () parens to reference List objects,
whereas Java Array classes use the [] square brackets . A Java List is “dynamic”
(luckily, we have discussed static versus dynamic); that is, it's open-ended, whereas a
Java Array is “static,” or fixed, which means that its length needs to be defined when it
is created.
Objects that implement List<E> start their numbering schema using a zero, just like
all Java Array objects. This is not out of the ordinary, as most Java programming con-
structs will also start counting at zero instead of using one. It is important to note from
an optimization standpoint that iterating over the elements in List<E> is typically
preferable (more optimal) to indexing through it by number, for instance using a for
loop, which is probably why support for this ListIterator interface is a part of the
List<E> interface specification, and therefore is a part of the ArrayList class, which has
no choice but to implement the List<E> interface specification because it uses the Java
implements keyword.
The List<E> interface also provides three methods allowing access to the List using
a specified object. These include .indexOf(Object object) , .con-
tains(Object object) , and .remove(Object object) . Again, looking at
it strictly from a performance standpoint, these methods should be used with caution,
because having to compare an “input” object to every object inside of the List is going
to take way more memory and CPU cycles than simply using the index for the object in
Search WWH ::




Custom Search