Name
AbstractList<E>
Synopsis
This abstract class is a partial
implementation of the List
interface that makes it
easy to define custom List
implementations based
on random-access list elements (such as objects stored in an array).
If you want to base a List
implementation on a
sequential-access data model (such as a linked list), subclass
AbstractSequentialList
instead.
To
create an unmodifiable List
, simply subclass
AbstractList
and override the (inherited)
size( )
and get( )
methods. To
create a modifiable list, you must also override set(
)
and, optionally, add( )
and
remove( )
. These three methods are optional, so
unless you override them, they simply throw an
UnsupportedOperationException
. All other methods
of the List
interface are implemented in terms of
size( )
, get( )
, set(
)
, add( )
, and remove(
)
. In some cases, you may want to override these other
methods to improve performance. By convention, all
List
implementations should define two
constructors: one that accepts no arguments and another that accepts
a Collection
of initial elements for the list.
Figure 16-2. java.util.AbstractList<E>
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { // Protected Constructors protected AbstractList( ); // Methods Implementing List public boolean add(E o); public void add(int index, E element); public boolean addAll(int index, Collection<? extends ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.