Java Reference
In-Depth Information
Chapter 15
Implementing
a Collection Class
Introduction
15.1 Simple ArrayIntList
Adding and Printing
In Chapters 10 and 11 we saw how to use various data structures that
are part of the collections framework.As a Java programmer, you will find
it useful to have these off-the-shelf solutions available to you, but you can
learn a lot from examining how these structures are implemented. In this
chapter we will explore in detail how to implement one such structure.
Thinking about Encapsulation
Dealing with the Middle of the
List
Another Constructor and a
Constant
Preconditions and
Postconditions
Our goal is to understand the ArrayList class that was described in
detail in Chapter 10. Recall that ArrayList is a generic class that is best
described as ArrayList<E> , where E is filled in with a specific element
type. Because generic classes are not easy to implement, we will first
explore the implementation of a class called ArrayIntList that can be
used to store a list of simple integer values.There aren't a lot of applica-
tions for a simple ArrayIntList object, but the code we write for it will
be similar to the code we need for the generic version.The chapter ends
with a section that explores how to turn the more specific
ArrayIntList into a generic ArrayList<E> .
15.2 A More Complete
ArrayIntList
Throwing Exceptions
Convenience Methods
15.3 Advanced Features
Resizing When Necessary
Adding an Iterator
15.4 ArrayList<E>
886
 
Search WWH ::




Custom Search