Class java.util.LinkedList
java.lang.Object
|
+----java.util.AbstractCollection
|
+----java.util.AbstractList
|
+----java.util.AbstractSequentialList
|
+----java.util.LinkedList
- public class LinkedList
- extends AbstractSequentialList
- implements List, Cloneable, Serializable
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, LinkedList provides
uniformly named methods to get, remove and insert an element at the
beginning and end of the List. These operations allow LinkedList 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 LinkedList concurrently, and at least one of the
threads modifies the LinkedList 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
LinkedList. If no such object exists, the LinkedList should be "wrapped"
using the Collections.synchronizedSet method. This is best done at
creation time, to prevent accidental unsynchronized access to the
LinkedList:
List list = Collections.synchronizedList(new LinkedList(...));
The Iterators returned by LinkedList's iterator and listIterator
methods are fail-fast: if the LinkedList 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.
- Since:
- JDK1.2
- See Also:
- List, ArrayList, Vector, Collections.synchronizedList
Constructor Summary
|
LinkedList()
Constructs an empty Linked List.
|
LinkedList(Collection c)
Constructs a LinkedList containing the elements of the specified
Collection, in the order they are returned by the Collection's
iterator.
|
Method Summary
|
void
|
addFirst(Object o)
Inserts the given element at the beginning of this List.
|
void
|
addLast(Object o)
Appends the given element to the end of this List.
|
void
|
clear()
Removes all of the elements from this List.
|
Object
|
clone()
Returns a shallow copy of this LinkedList.
|
Object
|
getFirst()
Returns the first Element in this List.
|
Object
|
getLast()
Returns the last Element in this List.
|
ListIterator
|
listIterator(int index)
Returns a ListIterator of the elements in this List (in proper
sequence), starting at the specified position in the list.
|
Object
|
removeFirst()
Removes and returns the first Element from this List.
|
Object
|
removeLast()
Removes and returns the last Element from this List.
|
int
|
size()
Returns the number of elements in this List.
|
Methods inherited from class java.util.AbstractList
|
add, add, addAll, equals, get, hashCode, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set |
Methods inherited from class java.util.AbstractCollection
|
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString |
Methods inherited from class java.lang.Object
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
LinkedList
public LinkedList()
- Constructs an empty Linked List.
LinkedList
public LinkedList(Collection c)
- Constructs a LinkedList containing the elements of the specified
Collection, in the order they are returned by the Collection's
iterator.
getFirst
public Object getFirst()
- Returns the first Element in this List.
getLast
public Object getLast()
- Returns the last Element in this List.
- Throws:
- NoSuchElementException - List is empty.
removeFirst
public Object removeFirst()
- Removes and returns the first Element from this List.
- Returns:
- the first Element from this List.
- Throws:
- NoSuchElementException - List is empty.
removeLast
public Object removeLast()
- Removes and returns the last Element from this List.
- Returns:
- the last Element from this List.
- Throws:
- NoSuchElementException - List is empty.
addFirst
public void addFirst(Object o)
- Inserts the given element at the beginning of this List.
addLast
public void addLast(Object o)
- Appends the given element to the end of this List. (Identical
in function to add(); included only for consistency.)
size
public int size()
- Returns the number of elements in this List.
- Implements:
- size in interface List
- Overrides:
- size in class AbstractCollection
clear
public void clear()
- Removes all of the elements from this List.
- Implements:
- clear in interface List
- Overrides:
- clear in class AbstractCollection
listIterator
public ListIterator listIterator(int index)
- Returns a ListIterator of the elements in this List (in proper
sequence), starting at the specified position in the list.
Obeys the general contract of List.listIterator(int).
The ListIterator is fail-fast: if the LinkedList is
structurally modified at any time after the Iterator is created, in
any way except through the ListIterator'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.
- Implements:
- listIterator in interface List
- Parameters:
index
- index of first element to be returned from the
ListIterator (by a call to getNext).
- Throws:
- IndexOutOfBoundsException - index is out of range
(index < 0 || index > size()).
- Overrides:
- listIterator in class AbstractSequentialList
- See Also:
- listIterator(int)
clone
public Object clone()
- Returns a shallow copy of this LinkedList. (The elements themselves
are not cloned.)
- Overrides:
- clone in class Object
Submit a bug or feature
Submit comments/suggestions about new javadoc look.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. All Rights Reserved.