Java Reference
In-Depth Information
1 package weiss.nonstandard;
2
3 // SimpleContainer protocol
4 public interface SimpleContainer<AnyType>
5 {
6 void insert( AnyType x );
7
figure 6.1
A generic protocol for
many data structures
void remove( AnyType x );
AnyType find( AnyType x );
8
9
10
boolean isEmpty( );
void makeEmpty( );
11
12 }
some competing implementations for data structures that follow the simple
protocols developed in this chapter. We will also provide one implementation
for the basic Collections API components described in the chapter, in package
weiss.util . Thus we are separating the interface of the Collections API (that is,
what it does, which we describe in the chapter) from its implementation (that
is, how it is done, which we describe in Part Four). This approach—the separa-
tion of the interface and implementation—is part of the object-oriented paradigm.
The user of the data structure needs to see only the available operations, not the
implementation. Recall this is the encapsulation and information-hiding part of
object-oriented programming.
The rest of this chapter is organized as follows: First, we discuss the
basics of the iterator pattern , which is used throughout the Collections API.
Then we discuss the interface for containers and iterators in the Collections
API. Next we describe some Collections API algorithms, and finally, we
examine some other data structures many of which are supported in the
Collections API.
the iterator pattern
6.2
The Collections API makes heavy use of a common technique known as the
iterator pattern. So before we begin our discussion of the Collections API, we
examine the ideas behind the iterator pattern.
Consider the problem of printing the elements in a collection. Typically,
the collection is an array, so assuming that the object v is an array, its contents
are easily printed with code like the following: 1
An iterator object
controls iteration
of a collection.
1. The enhanced for loop added in Java 5 is simply additional syntax. The compiler expands
the enhanced for loop to obtain the code shown here.
 
 
 
Search WWH ::




Custom Search