[all packages] [package java.util] [class hierarchy] [index]

public class java.util.LinkedList<A>

(source file: LinkedList.java)
java.lang.Object
   |
   +----java.util.AbstractCollection<A>
           |
           +----java.util.AbstractList<A>
                   |
                   +----java.util.AbstractSequentialList<A>
                           |
                           +----java.util.LinkedList<A>

The pure class interface.
public class LinkedList<A>
  extends AbstractSequentialList<A>
  implements List<A>, Cloneable, java.io.Serializable
Resizable array 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.

See also:
List, ArrayList, Vector, Collections.synchronizedList

Constuctor Index

O LinkedList(Collection<A>)
Constructs a LinkedList containing the elements of the specified Collection, in the order
O LinkedList()
Constructs an empty Linked List.

Methods

O addFirst(A)
Inserts the given element at the beginning of this List.
O addLast(A)
Appends the given element to the end of this List
O clear()
Removes all of the elements from this List.
O clone()
Returns a shallow copy of this LinkedList
O getFirst()
Returns the first Element in this List.
O getLast()
Returns the last Element in this List.
O listIterator(int)
Returns a ListIterator of the elements in this List (in proper sequence), starting at the
O removeFirst()
Removes and returns the first Element from this List.
O removeLast()
Removes and returns the last Element from this List.
O size()
Returns the number of elements in this List.

Inner Class Index

Entry<A>
O ListItr

Constructors

O LinkedList
public LinkedList();
Constructs an empty Linked List.

O LinkedList

public LinkedList(Collection<A> c);
Constructs a LinkedList containing the elements of the specified Collection, in the order they are returned by the Collection's iterator.

Methods

O getFirst
public A getFirst();
Returns the first Element in this List.

O getLast

public A getLast();
Returns the last Element in this List.

Throws:
NoSuchElementException -List is empty.

O removeFirst

public A removeFirst();
Removes and returns the first Element from this List.

Returns:
the first Element from this List.
Throws:
NoSuchElementException -List is empty.

O removeLast

public A removeLast();
Removes and returns the last Element from this List.

Returns:
the last Element from this List.
Throws:
NoSuchElementException -List is empty.

O addFirst

public void addFirst(A o);
Inserts the given element at the beginning of this List.

O addLast

public void addLast(A o);
Appends the given element to the end of this List. (Identical in function to add(); included only for consistency.)

O size

public int size();
Returns the number of elements in this List.

Overrides:
size in class AbstractCollection

O clear

public void clear();
Removes all of the elements from this List.

Overrides:
clear in class AbstractCollection

O listIterator

public ListIterator<A> 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.

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)

O clone

public Object clone();
Returns a shallow copy of this LinkedList. (The elements themselves are not cloned.)

Overrides:
clone in class Object

Inner Classes

O private class ListItr
private class ListItr
  implements ListIterator<A>

private static class Entry<A>

private static class Entry<A>


[
all packages] [package java.util] [class hierarchy] [index]
java.util.LinkedList.html