Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 682
نمايش فراداده

AbstractList<E>java.util

Java 1.2collection

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 E>

c ); public void

clear ( ); public boolean

equals (Object

o ); public abstract E

get (int

index ); public int

hashCode ( ); public int

indexOf (Object

o ); public Iterator<E>

iterator ( ); public int

lastIndexOf (Object

o ); public ListIterator<E>

listIterator ( ); public ListIterator<E>

listIterator (int

index ); public E

remove (int

index ); public E

set (int

index , E

element ); public List<E>

subList (int

fromIndex , int

toIndex ); // Protected Instance Methods protected void

removeRange (int

fromIndex , int

toIndex ); // Protected Instance Fields protected transient int

modCount ; }

Subclasses

AbstractSequentialList, ArrayList, Vector