Java Reference
In-Depth Information
EXERCISES
You can download the source code for the examples in the topic and the solutions to the follow-
ing exercises from www.wrox.com .
1. Implement a version of the program to calculate prime numbers that you saw in Chapter 4 to use a
Vector<> object instead of an array to store the primes. (Hint: Remember the Integer class.)
2. Write a program to store a deck of 52 cards in a linked list in random sequence using a Random class
object. You can represent a card as a two-character string — "1C" for the ace of clubs, "JD" for the
jack of diamonds, and so on. Output the cards from the linked list as four hands of 13 cards.
3. Extend the program from this chapter that used a map to store names and telephone numbers such
that you can enter a number to retrieve the name.
4. Implement a phone book so that just a surname can be used to search and have all the entries cor-
responding to the name display.
• WHAT YOU LEARNED IN THIS CHAPTER
TOPIC
CONCEPT
Collections
Framework
The Java collections framework provides you with a range of collection classes implemented as generic
types. These enable you to organize your data in various ways.
EnumSet<E> The EnumSet<> collection class enables you to aggregate one or more constants from a given enumera-
tion type into a set so they can be passed to a method as a single argument.
Vector<T> You can use a Vector<> object as a kind of flexible array that expands automatically to accommodate
any number of objects stored.
ArrayList<T> An ArrayList<> is very similar to a vector. The primary difference between them is that a Vector<> is
thread-safe, whereas an ArrayList<> is not.
The Stack<> class is derived from the Vector class and implements a pushdown stack.
Stack<T>
The HashMap<> class defines a hash map in which objects are stored based on an associated key.
HashMap<K,
V>
Iterators An Iterator<> is an interface for retrieving objects from a collection sequentially. An Iterator<>
object enables you to access all the objects it contains serially — but only once. There's no way to go
back to the beginning.
List Iterators The ListIterator<> interface provides methods for traversing the objects in a collection backward
and forward.
Using IteratorsObjects stored in any type of collection can be accessed using Iterator<> objects.
Using List
Iterators
Objects stored in a Vector<> , a Stack<>, or a LinkedList<> can be accessed using ListIterator<>
objects.
Search WWH ::




Custom Search