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
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:- Original author unknown
- Bryce McKinlay
- Eric Blake <ebb9@email.byu.edu>
See Also:
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:
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:
- true, as it always succeeds
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:
- true if the list was modified
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:
- true if the list was modified
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:
- an object of the same class as this object, containing the
same elements in the same order
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:
- its position, or -1 if not found
lastIndexOf
public int lastIndexOf(java.lang.Object o)
Returns the last index where the element is located in the list, or -1.
Parameters:
Returns:
- its position, or -1 if not found
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:
- true if an instance of the object was removed
removeFirst
public Object removeFirst()
Remove and return the first element in the list.
Returns:
- the former first element in the list
Throws:
removeLast
public Object removeLast()
Remove and return the last element in the list.
Returns:
- the former last element in the list
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:
- an array containing the list elements
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:
- an array representation of this list
Throws:
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.