java::util::LinkedList Class Reference

Inheritance diagram for java::util::LinkedList:

Inheritance graph
java::util::AbstractSequentialListjava::util::AbstractListjava::util::AbstractCollectionjava::util::Collectionjava::util::Listjava::lang::Interfacejava::lang::Object
[legend]
Collaboration diagram for java::util::LinkedList:

Collaboration graph
java::util::AbstractSequentialListjava::util::AbstractListjava::util::AbstractCollectionjava::util::Collectionjava::util::Listjava::lang::Interfacejava::lang::Comparablejava::lang::Objectjava::lang::ObjectRef
[legend]

List of all members.


Detailed Description

Linked list implementation of the List interface.

Implements all optional list operations, and permits all elements (including null). In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).

All of the stack/queue/deque operations could be easily recast in terms of the standard list operations. They're included here primarily for convenience, though they may run slightly faster than the equivalent List operations.

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the begining or the end, whichever is closer to the specified index.

Note that this implementation is not synchronized. If multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

     List list = Collections.synchronizedList(new LinkedList(...));
 

The iterators returned by the this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

Author:
Josh Bloch
Version:
1.43, 12/03/01
See also:
List

ArrayList

Vector

Collections::synchronizedList(List)

Since:
1.2

Public Member Functions

 LinkedList ()
 Constructs an empty list.
 LinkedList (const Collection &c)
 Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
 LinkedList (const LinkedList &ll)
virtual ObjectRef getFirst () const
 Returns the first element in this list.
virtual ObjectRef getLast () const
 Returns the last element in this list.
virtual ObjectRef removeFirst ()
 Removes and returns the first element from this list.
virtual ObjectRef removeLast ()
 Removes and returns the last element from this list.
virtual void addFirst (const ObjectRef &o)
 Inserts the given element at the beginning of this list.
virtual void addLast (const ObjectRef &o)
 Appends the given element to the end of this list.
virtual jboolean contains (const ObjectRef &o) const
 Returns true if this collection contains the specified element.
virtual jint size () const
 Returns the number of elements in this list.
virtual jboolean add (const ObjectRef &o)
 Appends the specified element to the end of this list.
virtual jboolean remove (const ObjectRef &o)
 Removes the first occurrence of the specified element in this list.
virtual jboolean addAll (const Collection &c)
 Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
virtual jboolean addAll (jint index, const Collection &c)
 Inserts all of the elements in the specified collection into this list, starting at the specified position.
virtual void clear ()
 Removes all of the elements from this list.
virtual ObjectRef get (jint index) const
 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.
virtual void add (jint index, const ObjectRef &element)
 Inserts the specified element at the specified position in this list.
virtual ObjectRef remove (jint index)
 Removes the element at the specified position in this list.
virtual jint indexOf (const ObjectRef &o) const
 Returns the index in this list of the first occurrence of the specified element, or -1 if the 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 the list does not contain this element.
virtual Array
< ObjectRef
toArray () const
 Returns an array containing all of the elements in this list in the correct order.
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.

Constructor & Destructor Documentation

java::util::LinkedList::LinkedList (  ) 

Constructs an empty list.

java::util::LinkedList::LinkedList ( const Collection c  ) 

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Parameters:
c the collection whose elements are to be placed into this list.
Exceptions:
NullPointerException if the specified collection is null.

java::util::LinkedList::LinkedList ( const LinkedList ll  ) 


Member Function Documentation

virtual ObjectRef java::util::LinkedList::getFirst (  )  const [virtual]

Returns the first element in this list.

Returns:
the first element in this list.
Exceptions:
NoSuchElementException if this list is empty.

virtual ObjectRef java::util::LinkedList::getLast (  )  const [virtual]

Returns the last element in this list.

Returns:
the last element in this list.
Exceptions:
NoSuchElementException if this list is empty.

virtual ObjectRef java::util::LinkedList::removeFirst (  )  [virtual]

Removes and returns the first element from this list.

Returns:
the first element from this list.
Exceptions:
NoSuchElementException if this list is empty.

virtual ObjectRef java::util::LinkedList::removeLast (  )  [virtual]

Removes and returns the last element from this list.

Returns:
the last element from this list.
Exceptions:
NoSuchElementException if this list is empty.

virtual void java::util::LinkedList::addFirst ( const ObjectRef o  )  [virtual]

Inserts the given element at the beginning of this list.

Parameters:
o the element to be inserted at the beginning of this list.

virtual void java::util::LinkedList::addLast ( const ObjectRef o  )  [virtual]

Appends the given element to the end of this list.

(Identical in function to the add method; included only for consistency.)

Parameters:
o the element to be inserted at the end of this list.

virtual jboolean java::util::LinkedList::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::AbstractList.

virtual jint java::util::LinkedList::size (  )  const [virtual]

Returns the number of elements in this list.

Returns:
the number of elements in this list.

Implements java::util::Collection.

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

Appends the specified element to the end of this list.

Parameters:
o element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

Reimplemented from java::util::AbstractList.

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

Removes the first occurrence of the specified element in this list.

If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Parameters:
o element to be removed from this list, if present.
Returns:
true if the list contained the specified element.

Reimplemented from java::util::AbstractList.

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

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

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 list, and this list is nonempty.)

Parameters:
c the elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Exceptions:
NullPointerException if the specified collection is null.

Reimplemented from java::util::AbstractList.

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

Inserts all of the elements in the specified collection into this list, starting at the specified position.

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 the list in the order that they are returned by the specified collection's iterator.

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:
IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size()).
NullPointerException if the specified collection is null.

Reimplemented from java::util::AbstractSequentialList.

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

Removes all of the elements from this list.

Reimplemented from java::util::AbstractList.

virtual ObjectRef java::util::LinkedList::get ( jint  index  )  const [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 specified index is is out of range (index < 0 || index >= size()).

Reimplemented from java::util::AbstractSequentialList.

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

Replaces the element at the specified position in this list with the specified element.

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:
IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).

Reimplemented from java::util::AbstractSequentialList.

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

Inserts the specified element at the specified position in this list.

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:
IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size()).

Reimplemented from java::util::AbstractSequentialList.

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

Removes the element at the specified position in this list.

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:
IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).

Reimplemented from java::util::AbstractSequentialList.

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

Returns the index in this list of the first occurrence of the specified element, or -1 if the 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 the list does not contain this element.

Reimplemented from java::util::AbstractList.

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

Returns the index in this list of the last occurrence of the specified element, or -1 if the 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 the list does not contain this element.

Reimplemented from java::util::AbstractList.

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

Returns an array containing all of the elements in this list in the correct order.

Returns:
an array containing all of the elements in this list in the correct order.

Reimplemented from java::util::AbstractList.

virtual Ref<ListIterator> java::util::LinkedList::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::AbstractSequentialList.


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