java::util::AbstractList Class Reference

Inheritance diagram for java::util::AbstractList:

Inheritance graph
java::util::AbstractSequentialListjava::util::ArrayListjava::util::AbstractCollectionjava::util::Collectionjava::util::Listjava::lang::Interfacejava::lang::Objectjava::util::LinkedList
[legend]
Collaboration diagram for java::util::AbstractList:

Collaboration graph
java::util::AbstractCollectionjava::util::Collectionjava::util::Listjava::lang::Interfacejava::lang::Object
[legend]

List of all members.


Detailed Description

This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array).

For sequential access data (such as a linked list), AbstractSequentialList should be used in preference to this class.

To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for the get(int index) and size() methods.

To implement a modifiable list, the programmer must additionally override the set(int index, Object element) method (which otherwise throws an UnsupportedOperationException. If the list is variable-size the programmer must additionally override the add(int index, Object element) and remove(int index) methods.

The programmer should generally provide a void (no argument) and collection constructor, as per the recommendation in the Collection interface specification.

Unlike the other abstract collection implementations, the programmer does not have to provide an iterator implementation; the iterator and list iterator are implemented by this class, on top the "random access" methods: get(int index), set(int index, Object element), set(int index, Object element), add(int index, Object element) and remove(int index).

The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the collection being implemented admits a more efficient implementation.

Author:
Josh Bloch
Version:
1.35, 12/03/01
See also:
Collection

List

AbstractSequentialList

AbstractCollection

Since:
1.2

Public Member Functions

 AbstractList ()
 Sole constructor.
virtual jboolean isEmpty () const
 Returns true if this collection contains no elements.
virtual jboolean contains (const ObjectRef &o) const
 Returns true if this collection contains the specified element.
virtual Array
< ObjectRef
toArray () const
 Returns an array containing all of the elements in this collection.
virtual jboolean add (const ObjectRef &o)
 Ensures that this collection contains the specified element (optional operation).
virtual void add (jint index, const ObjectRef &element)
 Inserts the specified element at the specified position in this list (optional operation).
virtual ObjectRef get (jint index) const =0
 Returns the element at the specified position in this list.
virtual ObjectRef set (jint index, const ObjectRef &element)
 Replaces the element at the specified position in this list with the specified element (optional operation).
virtual jboolean remove (const ObjectRef &o)
 Removes a single instance of the specified element from this collection, if it is present (optional operation).
virtual ObjectRef remove (jint index)
 Removes the element at the specified position in this list (optional operation).
virtual jint indexOf (const ObjectRef &o) const
 Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
virtual jint lastIndexOf (const ObjectRef &o) const
 Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
virtual jboolean containsAll (const Collection &c) const
 Returns true if this collection contains all of the elements in the specified collection.
virtual jboolean addAll (const Collection &c)
 Adds all of the elements in the specified collection to this collection (optional operation).
virtual jboolean addAll (jint index, const Collection &c)
 Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
virtual jboolean removeAll (const Collection &c)
 Removes from this collection all of its elements that are contained in the specified collection (optional operation).
virtual jboolean retainAll (const Collection &c)
 Retains only the elements in this collection that are contained in the specified collection (optional operation).
virtual void clear ()
 Removes all of the elements from this collection (optional operation).
virtual Ref< Iteratoriterator () const
 Returns an iterator over the elements in this collection.
virtual Ref
< ListIterator
listIterator (jint index=0) const
 Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
virtual Ref< ListsubList (jint fromIndex, jint toIndex) const
 Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
virtual jboolean equals (const Object &o) const
 Compares the specified object with this set for equality.
virtual jint hashCode () const
 Returns the hash code value for this set.
virtual String toString () const
 Returns a string representation of this set.
virtual void removeRange (jint fromIndex, jint toIndex)
 Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Constructor & Destructor Documentation

java::util::AbstractList::AbstractList (  ) 

Sole constructor.

(For invocation by subclass constructors, typically implicit.)


Member Function Documentation

virtual jboolean java::util::AbstractList::isEmpty (  )  const [virtual]

Returns true if this collection contains no elements.

This implementation returns size() == 0.

Returns:
true if this collection contains no elements.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList.

virtual jboolean java::util::AbstractList::contains ( const ObjectRef o  )  const [virtual]

Returns true if this collection contains the specified element.

More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.

Parameters:
o object to be checked for containment in this collection.
Returns:
true if this collection contains the specified element.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual Array<ObjectRef> java::util::AbstractList::toArray (  )  const [virtual]

Returns an array containing all of the elements in this collection.

If the collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by the collection. (In other words, this method must allocate a new array even if the collection is backed by an Array). The caller is thus free to modify the returned array.

This implementation allocates the array to be returned, and iterates over the elements in the collection, storing each object reference in the next consecutive element of the array, starting with element 0.

Returns:
an array containing all of the elements in this collection.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual jboolean java::util::AbstractList::add ( const ObjectRef o  )  [virtual]

Ensures that this collection contains the specified element (optional operation).

Returns true if the collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.) Collections that support this operation may place limitations on what elements may be added to the collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

This implementation always throws an UnsupportedOperationException.

Parameters:
o element whose presence in this collection is to be ensured.
Returns:
true if the collection changed as a result of the call.
Exceptions:
UnsupportedOperationException if the add method is not supported by this collection.
NullPointerException if this collection does not permit null elements, and the specified element is null.
ClassCastException if the class of the specified element prevents it from being added to this collection.
IllegalArgumentException if some aspect of this element prevents it from being added to this collection.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual void java::util::AbstractList::add ( jint  index,
const ObjectRef element 
) [virtual]

Inserts the specified element at the specified position in this list (optional operation).

Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index index at which the specified element is to be inserted.
element element to be inserted.
Exceptions:
UnsupportedOperationException if the add method is not supported by this list.
ClassCastException if the class of the specified element prevents it from being added to this list.
NullPointerException if the specified element is null and this list does not support null elements.
IllegalArgumentException if some aspect of the specified element prevents it from being added to this list.
IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).

Implements java::util::List.

Reimplemented in java::util::AbstractSequentialList, java::util::ArrayList, and java::util::LinkedList.

virtual ObjectRef java::util::AbstractList::get ( jint  index  )  const [pure virtual]

Returns the element at the specified position in this list.

Parameters:
index index of element to return.
Returns:
the element at the specified position in this list.
Exceptions:
IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).

Implements java::util::List.

Implemented in java::util::AbstractSequentialList, java::util::ArrayList, and java::util::LinkedList.

virtual ObjectRef java::util::AbstractList::set ( jint  index,
const ObjectRef element 
) [virtual]

Replaces the element at the specified position in this list with the specified element (optional operation).

Parameters:
index index of element to replace.
element element to be stored at the specified position.
Returns:
the element previously at the specified position.
Exceptions:
UnsupportedOperationException if the set method is not supported by this list.
ClassCastException if the class of the specified element prevents it from being added to this list.
NullPointerException if the specified element is null and this list does not support null elements.
IllegalArgumentException if some aspect of the specified element prevents it from being added to this list.
IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).

Implements java::util::List.

Reimplemented in java::util::AbstractSequentialList, java::util::ArrayList, and java::util::LinkedList.

virtual jboolean java::util::AbstractList::remove ( const ObjectRef o  )  [virtual]

Removes a single instance of the specified element from this collection, if it is present (optional operation).

More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the collection contains one or more such elements. Returns true if the collection contained the specified element (or equivalently, if the collection changed as a result of the call).

This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method.

Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object.

Parameters:
o element to be removed from this collection, if present.
Returns:
true if the collection contained the specified element.
Exceptions:
UnsupportedOperationException if the remove method is not supported by this collection.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual ObjectRef java::util::AbstractList::remove ( jint  index  )  [virtual]

Removes the element at the specified position in this list (optional operation).

Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters:
index the index of the element to removed.
Returns:
the element previously at the specified position.
Exceptions:
UnsupportedOperationException if the remove method is not supported by this list.
IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()).

Implements java::util::List.

Reimplemented in java::util::AbstractSequentialList, java::util::ArrayList, and java::util::LinkedList.

virtual jint java::util::AbstractList::indexOf ( const ObjectRef o  )  const [virtual]

Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.

More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o element to search for.
Returns:
the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
Exceptions:
ClassCastException if the type of the specified element is incompatible with this list (optional).
NullPointerException if the specified element is null and this list does not support null elements (optional).

Implements java::util::List.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual jint java::util::AbstractList::lastIndexOf ( const ObjectRef o  )  const [virtual]

Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.

More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o element to search for.
Returns:
the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
Exceptions:
ClassCastException if the type of the specified element is incompatible with this list (optional).
NullPointerException if the specified element is null and this list does not support null elements (optional).

Implements java::util::List.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual jboolean java::util::AbstractList::containsAll ( const Collection c  )  const [virtual]

Returns true if this collection contains all of the elements in the specified collection.

This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it's contained in this collection. If all elements are so contained true is returned, otherwise false.

Parameters:
c collection to be checked for containment in this collection.
Returns:
true if this collection contains all of the elements in the specified collection.
Exceptions:
NullPointerException if the specified collection is null.
See also:
contains(Object)

Reimplemented from java::util::AbstractCollection.

virtual jboolean java::util::AbstractList::addAll ( const Collection c  )  [virtual]

Adds all of the elements in the specified collection to this collection (optional operation).

The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)

This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn.

Note that this implementation will throw an UnsupportedOperationException unless add is overridden (assuming the specified collection is non-empty).

Parameters:
c collection whose elements are to be added to this collection.
Returns:
true if this collection changed as a result of the call.
Exceptions:
UnsupportedOperationException if this collection does not support the addAll method.
NullPointerException if the specified collection is null.
See also:
add(Object)

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual jboolean java::util::AbstractList::addAll ( jint  index,
const Collection c 
) [virtual]

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).

Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
index index at which to insert first element from the specified collection.
c elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Exceptions:
UnsupportedOperationException if the addAll method is not supported by this list.
ClassCastException if the class of one of elements of the specified collection prevents it from being added to this list.
NullPointerException if the specified collection contains one or more null elements and this list does not support null elements, or if the specified collection is null.
IllegalArgumentException if some aspect of one of elements of the specified collection prevents it from being added to this list.
IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).

Implements java::util::List.

Reimplemented in java::util::AbstractSequentialList, java::util::ArrayList, and java::util::LinkedList.

virtual jboolean java::util::AbstractList::removeAll ( const Collection c  )  [virtual]

Removes from this collection all of its elements that are contained in the specified collection (optional operation).

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's so contained, it's removed from this collection with the iterator's remove method.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements in common with the specified collection.

Parameters:
c elements to be removed from this collection.
Returns:
true if this collection changed as a result of the call.
Exceptions:
UnsupportedOperationException if the removeAll method is not supported by this collection.
NullPointerException if the specified collection is null.
See also:
remove(Object)

contains(Object)

Reimplemented from java::util::AbstractCollection.

virtual jboolean java::util::AbstractList::retainAll ( const Collection c  )  [virtual]

Retains only the elements in this collection that are contained in the specified collection (optional operation).

In other words, removes from this collection all of its elements that are not contained in the specified collection.

This implementation iterates over this collection, checking each element returned by the iterator in turn to see if it's contained in the specified collection. If it's not so contained, it's removed from this collection with the iterator's remove method.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by the iterator method does not implement the remove method and this collection contains one or more elements not present in the specified collection.

Parameters:
c elements to be retained in this collection.
Returns:
true if this collection changed as a result of the call.
Exceptions:
UnsupportedOperationException if the retainAll method is not supported by this Collection.
NullPointerException if the specified collection is null.
See also:
remove(Object)

contains(Object)

Reimplemented from java::util::AbstractCollection.

virtual void java::util::AbstractList::clear (  )  [virtual]

Removes all of the elements from this collection (optional operation).

The collection will be empty after this call returns (unless it throws an exception).

This implementation iterates over this collection, removing each element using the Iterator.remove operation. Most implementations will probably choose to override this method for efficiency.

Note that this implementation will throw an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection is non-empty.

Exceptions:
UnsupportedOperationException if the clear method is not supported by this collection.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList, and java::util::LinkedList.

virtual Ref<Iterator> java::util::AbstractList::iterator (  )  const [virtual]

Returns an iterator over the elements in this collection.

There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

Returns:
an Iterator over the elements in this collection

Implements java::util::Collection.

virtual Ref<ListIterator> java::util::AbstractList::listIterator ( jint  index = 0  )  const [virtual]

Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.

The specified index indicates the first element that would be returned by an initial call to the next method. An initial call to the previous method would return the element with the specified index minus one.

Parameters:
index index of first element to be returned from the list iterator (by a call to the next method).
Returns:
a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
Exceptions:
IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).

Implements java::util::List.

Reimplemented in java::util::AbstractSequentialList, and java::util::LinkedList.

virtual Ref<List> java::util::AbstractList::subList ( jint  fromIndex,
jint  toIndex 
) const [virtual]

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

(If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

	    list.subList(from, to).clear();
 
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Parameters:
fromIndex low endpoint (inclusive) of the subList.
toIndex high endpoint (exclusive) of the subList.
Returns:
a view of the specified range within this list.
Exceptions:
IndexOutOfBoundsException for an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex).

Implements java::util::List.

virtual jboolean java::util::AbstractList::equals ( const Object o  )  const [virtual]

Compares the specified object with this set for equality.

Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.

This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it it returns false. If so, it returns containsAll((Collection) o).

Parameters:
o Object to be compared for equality with this set.
Returns:
true if the specified object is equal to this set.

Reimplemented from java::lang::Object.

Reimplemented in java::util::ArrayList.

virtual jint java::util::AbstractList::hashCode (  )  const [virtual]

Returns the hash code value for this set.

The hash code of a set is defined to be the sum of the hash codes of the elements in the set. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of Object.hashCode.

This implementation enumerates over the set, calling the hashCode method on each element in the collection, and adding up the results.

Returns:
the hash code value for this set.

Reimplemented from java::lang::Object.

Reimplemented in java::util::ArrayList.

virtual String java::util::AbstractList::toString (  )  const [virtual]

Returns a string representation of this set.

The string representation consists of a list of the set's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.

Returns:
a string representation of this set.

Reimplemented from java::util::AbstractCollection.

Reimplemented in java::util::ArrayList.

virtual void java::util::AbstractList::removeRange ( jint  fromIndex,
jint  toIndex 
) [virtual]

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Shifts any succeeding elements to the left (reduces their index). This call shortens the ArrayList by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

This method is called by the clear operation on this list and its subLists. Overriding this method to take advantage of the internals of the list implementation can substantially improve the performance of the clear operation on this list and its subLists.

This implementation gets a list iterator positioned before fromIndex, and repeatedly calls ListIterator.next followed by ListIterator.remove until the entire range has been removed. Note: if ListIterator.remove requires linear time, this implementation requires quadratic time.

Parameters:
fromIndex index of first element to be removed.
toIndex index after last element to be removed.

Reimplemented in java::util::ArrayList.


The documentation for this class was generated from the following file:
Generated on Fri May 16 11:56:46 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3