Game Development Reference
In-Depth Information
the List. After all, that is what an index is for! In many implementations this will per-
form a costly “linear” object search, starting at ListElement[0] and going through the
entire list comparing objects. If your 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 List containing a
great many object elements, you may well observe a performance hit using these “ob-
ject oriented” methods. Well, all methods are object oriented, so, let's cleverly call
these methods “object parameterized” instead!
The List<E> interface also provides two methods to efficiently read or remove
multiple List elements at an arbitrary point within the List. The .re-
moveRange(int fromIndex, int toIndex) removes a range of List ele-
ments, and the .subList(int fromIndex, int toIndex) returns a view of
the portion of the List between the specified fromIndex and the toIndex. The fromIn-
dex is included in the returned sub-list, however, the toIndex is not included.
Finally, the List<E> interface provides three methods that manipulate the entire
List using a single method. Since we are using the List to manage all of the Actor ob-
jects currently in the scene, we will primarily be using these methods, which were
mentioned earlier in the ArrayList section of the chapter and include .addAll(), .re-
moveAll(), and .clear(). We will also be using the .add(E element) method to add
a single Actor object to our CURRENT_CAST List.
Finally, while it is technically permissible for a List<E> to contain itself as an ele-
ment, this is not viewed as being a “good” programming practice, so I do not recom-
mend doing this. You should use extreme caution if you are going to try doing this, be-
cause the equals and hashCode methods will no longer be “well defined” in such a
List.
Set and HashSet: Using java.util
Unordered Sets
The Set<E> public interface is also a member of the Java Collections Framework.
The Java public interface Set<E> extends the Collections<E> public interface, which
extends the Iterable<T> public interface. Thus, the super interface to sub interface hier-
archy for Set<E> is the same as it is for List<E> and looks like the following interface
hierarchy:
Search WWH ::




Custom Search