Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class LinkedList

java.lang.Object
|
+--java.util.AbstractCollection
   |
   +--java.util.AbstractList
      |
      +--java.util.AbstractSequentialList
         |
         +--java.util.LinkedList

All Implemented Interfaces:

List, Cloneable, Serializable, List, Collection


public class LinkedList

extends AbstractSequentialList

implements List, Cloneable, Serializable

Linked list implementation of the List interface. In addition to the methods of the List interface, this class provides access to the first and last list elements in O(1) time for easy stack, queue, or double-ended queue (deque) creation. The list is doubly-linked, with traversal to a given index starting from the end closest to the element.

LinkedList is not synchronized, so if you need multi-threaded access, consider using:
List l = Collections.synchronizedList(new LinkedList(...));

The iterators are fail-fast, meaning that any structural modification, except for remove() called on the iterator itself, cause the iterator to throw a ConcurrentModificationException rather than exhibit non-deterministic behavior.

Since:Authors:See Also:

Constructor Summary

LinkedList()

Create an empty linked list.
LinkedList(java.util.Collection c)

Create a linked list containing the elements, in order, of a given collection.

Method Summary

booleanadd(java.lang.Object o)

Adds an element to the end of the list.
voidadd(int index, java.lang.Object o)

Inserts an element in the given position in the list.
booleanaddAll(java.util.Collection c)

Append the elements of the collection in iteration order to the end of this list.
booleanaddAll(int index, java.util.Collection c)

Insert the elements of the collection in iteration order at the given index of this list.
voidaddFirst(java.lang.Object o)

Insert an element at the first of the list.
voidaddLast(java.lang.Object o)

Insert an element at the last of the list.
voidclear()

Remove all elements from this list.
java.lang.Objectclone()

Create a shallow copy of this LinkedList (the elements are not cloned).
booleancontains(java.lang.Object o)

Returns true if the list contains the given object.
java.lang.Objectget(int index)

Return the element at index.
java.lang.ObjectgetFirst()

Returns the first element in the list.
java.lang.ObjectgetLast()

Returns the last element in the list.
intindexOf(java.lang.Object o)

Returns the first index where the element is located in the list, or -1.
intlastIndexOf(java.lang.Object o)

Returns the last index where the element is located in the list, or -1.
java.util.ListIteratorlistIterator(int index)

Obtain a ListIterator over this list, starting at a given index.
booleanremove(java.lang.Object o)

Removes the entry at the lowest index in the list that matches the given object, comparing by o == null ? e = null : o.equals(e).
java.lang.Objectremove(int index)

Removes the element at the given position from the list.
java.lang.ObjectremoveFirst()

Remove and return the first element in the list.
java.lang.ObjectremoveLast()

Remove and return the last element in the list.
java.lang.Objectset(int index, java.lang.Object o)

Replace the element at the given location in the list.
intsize()

Returns the size of the list.
java.lang.Object[]toArray()

Returns an array which contains the elements of the list in order.
java.lang.Object[]toArray(java.lang.Object[] a)

Returns an Array whose component type is the runtime component type of the passed-in Array.

Constructor Details

LinkedList

public LinkedList()

Create an empty linked list.


LinkedList

public LinkedList(java.util.Collection c)

Create a linked list containing the elements, in order, of a given collection.

Parameters:

Throws:


Method Details

add

public void add(int index, java.lang.Object o)

Inserts an element in the given position in the list.

Parameters:

Throws:


add

public boolean add(java.lang.Object o)

Adds an element to the end of the list.

Parameters:

Returns:


addAll

public boolean addAll(int index, java.util.Collection c)

Insert the elements of the collection in iteration order at the given index of this list. If this list is modified externally (for example, if this list is the collection), behavior is unspecified.

Parameters:

Returns:

Throws:


addAll

public boolean addAll(java.util.Collection c)

Append the elements of the collection in iteration order to the end of this list. If this list is modified externally (for example, if this list is the collection), behavior is unspecified.

Parameters:

Returns:

Throws:


addFirst

public void addFirst(java.lang.Object o)

Insert an element at the first of the list.

Parameters:


addLast

public void addLast(java.lang.Object o)

Insert an element at the last of the list.

Parameters:


clear

public void clear()

Remove all elements from this list.


clone

public Object clone()

Create a shallow copy of this LinkedList (the elements are not cloned).

Returns:


contains

public boolean contains(java.lang.Object o)

Returns true if the list contains the given object. Comparison is done by o == null ? e = null : o.equals(e).

Parameters:

Returns:


get

public Object get(int index)

Return the element at index.

Parameters:

Returns:

Throws:


getFirst

public Object getFirst()

Returns the first element in the list.

Returns:

Throws:


getLast

public Object getLast()

Returns the last element in the list.

Returns:

Throws:


indexOf

public int indexOf(java.lang.Object o)

Returns the first index where the element is located in the list, or -1.

Parameters:

Returns:


lastIndexOf

public int lastIndexOf(java.lang.Object o)

Returns the last index where the element is located in the list, or -1.

Parameters:

Returns:


listIterator

public ListIterator listIterator(int index)

Obtain a ListIterator over this list, starting at a given index. The ListIterator returned by this method supports the add, remove and set methods.

Parameters:

Throws:


remove

public Object remove(int index)

Removes the element at the given position from the list.

Parameters:

Returns:

Throws:


remove

public boolean remove(java.lang.Object o)

Removes the entry at the lowest index in the list that matches the given object, comparing by o == null ? e = null : o.equals(e).

Parameters:

Returns:


removeFirst

public Object removeFirst()

Remove and return the first element in the list.

Returns:

Throws:


removeLast

public Object removeLast()

Remove and return the last element in the list.

Returns:

Throws:


set

public Object set(int index, java.lang.Object o)

Replace the element at the given location in the list.

Parameters:

Returns:

Throws:


size

public int size()

Returns the size of the list.

Returns:


toArray

public Object[] toArray()

Returns an array which contains the elements of the list in order.

Returns:


toArray

public Object[] toArray(java.lang.Object[] a)

Returns an Array whose component type is the runtime component type of the passed-in Array. The returned Array is populated with all of the elements in this LinkedList. If the passed-in Array is not large enough to store all of the elements in this List, a new Array will be created and returned; if the passed-in Array is larger than the size of this List, then size() index will be set to null.

Parameters:

Returns:

Throws: