Java Reference
In-Depth Information
for(Person person : filmCast) {
System.out.println(person);
}
}
Directory "TryVector"
You'll need to add the following import statement to the TryVector.java file:
import java.util.Collections;
If you run the example with these changes, you get additional output with the cast in alphabetical order.
Here's what I got when I entered the same data as last time:
Input record and output exactly as before...
The cast in ascending sequence is:
Jennifer Aniston
George Clooney
Judy Dench
Johnny Depp
How It Works
Passing the filmCast object to the static sort() method in the Collections class sorts the objects in
the vector in place. Like shelling peas!
The sort() method is actually a parameterized method so it works for any type that implements the
Comparable<> interface. The way the type parameter for the method is defined is interesting:
static <T extends Comparable<? super T>> void sort(List<T> list)
Recall from the discussion of parameterized types in the previous chapter that using a wildcard with the
superclass constraint that you see here specifies that the type argument can be any type that implements
the Comparable<> interface or inherits an implementation from a superclass.
The method parameter is of type List<T> rather than Collection<T> because the List<T> interface
provides methods that allow the position where elements are inserted to be determined. It also provides
the listIterator() method that returns a ListIterator<T> object that allows iteration forward and
backward through the objects in the collection.
Stack Storage
A stack is a storage mechanism that works on a last-in, first-out basis, which, as you know, is often abbrevi-
ated to LIFO. Don't confuse this with FIFO, which is first-in, first-out, or FIFI, which is a name for a poodle.
The operation of a stack is analogous to the plate stack you see in some self-service restaurants and is illus-
trated in Figure 14-9 . The stack of plates is supported by a spring that allows the stack of plates to sink into
a hole in the countertop so that only the top plate is accessible. The plates come out in the reverse order to
the way they went in, so the cold plates are at the bottom, and the hot plates, fresh from the dishwasher, are
at the top, which is not so good if you want something chilled.
 
Search WWH ::




Custom Search