Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
public class LinkedList<T>
extends AbstractSequentialList<E>
implements List<E>, Deque, Cloneable, Serializable
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.
List
, ArrayList
, Vector
, Collections.synchronizedList(List)
, Serialized FormField Summary |
Fields inherited from class java.util.AbstractList<E> | |
modCount |
Constructor Summary | |
| |
|
Method Summary | |
|
|
boolean |
|
void |
|
boolean |
|
boolean |
|
void |
|
void |
|
void |
|
Object |
|
boolean | |
Iterator |
|
T |
|
T |
|
T |
|
T |
|
int | |
int |
|
ListIterator |
|
boolean |
|
boolean |
|
boolean |
|
T |
|
T |
|
T |
|
T |
|
T |
|
T |
|
T |
|
void |
|
T |
|
T |
|
boolean | |
T |
|
boolean |
|
T |
|
boolean |
|
T |
|
int |
|
Object[] |
|
Methods inherited from class java.util.AbstractSequentialList<E> | |
add , addAll , get , iterator , listIterator , remove , set |
Methods inherited from class java.util.AbstractList<E> | |
add , add , addAll , clear , equals , get , hashCode , indexOf , iterator , lastIndexOf , listIterator , listIterator , remove , removeRange , set , subList |
Methods inherited from class java.util.AbstractCollection<E> | |
T[] toArray , add , addAll , clear , contains , containsAll , isEmpty , iterator , remove , removeAll , retainAll , size , toArray , toString |
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public LinkedList(T> c)
Create a linked list containing the elements, in order, of a given collection.
- Parameters:
c
- the collection to populate this list from
- Throws:
NullPointerException
- if c is null
publicS[] toArray(S[] 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:a
- the passed-in Array
Returns:an array representation of this list
Throws:ArrayStoreException
- if the runtime type of a does not allow an element in this listNullPointerException
- if a is null
public boolean add(T o)
Adds an element to the end of the list.
- Parameters:
o
- the entry to add
- Returns:
- true, as it always succeeds
public void add(int index, T o)
Inserts an element in the given position in the list.
- Parameters:
index
- where to insert the elemento
- the element to insert
- Throws:
IndexOutOfBoundsException
- if index < 0 || index > size()
public boolean addAll(T> 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:
c
- the collection to append
- Returns:
- true if the list was modified
- Throws:
NullPointerException
- if c is null
public boolean addAll(int index, T> 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:
c
- the collection to append
- Returns:
- true if the list was modified
- Throws:
NullPointerException
- if c is nullIndexOutOfBoundsException
- if index < 0 || index > size()
public void addFirst(T o)
Insert an element at the first of the list.
- Parameters:
o
- the element to insert
public void addLast(T o)
Insert an element at the last of the list.
- Parameters:
o
- the element to insert
public void clear()
Remove all elements from this list.
- Specified by:
- clear in interface List<E>
- clear in interface Collection<E>
- Overrides:
- clear in interface AbstractList<E>
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
public boolean contains(Object o)
Returns true if the list contains the given object. Comparison is done byo == null ? e = null : o.equals(e)
.
- Specified by:
- contains in interface List<E>
- contains in interface Collection<E>
- Overrides:
- contains in interface AbstractCollection<E>
- Parameters:
o
- the element to look for
- Returns:
- true if it is found
public IteratordescendingIterator()
Obtain an Iterator over this list in reverse sequential order.
- Returns:
- an Iterator over the elements of the list in reverse order.
- Since:
- 1.6
public T element()
Returns the first element of the list without removing it.
- Returns:
- the first element of the list.
- Throws:
NoSuchElementException
- if the list is empty.
- Since:
- 1.5
public T get(int index)
Return the element at index.
- Overrides:
- get in interface AbstractSequentialList<E>
- Parameters:
index
- the place to look
- Returns:
- the element at index
- Throws:
IndexOutOfBoundsException
- if index < 0 || index >= size()
public T getFirst()
Returns the first element in the list.
- Returns:
- the first list element
- Throws:
NoSuchElementException
- if the list is empty
public T getLast()
Returns the last element in the list.
- Returns:
- the last list element
- Throws:
NoSuchElementException
- if the list is empty
public int indexOf(Object o)
Returns the first index where the element is located in the list, or -1.
- Overrides:
- indexOf in interface AbstractList<E>
- Parameters:
o
- the element to look for
- Returns:
- its position, or -1 if not found
public int lastIndexOf(Object o)
Returns the last index where the element is located in the list, or -1.
- Specified by:
- lastIndexOf in interface List<E>
- Overrides:
- lastIndexOf in interface AbstractList<E>
- Parameters:
o
- the element to look for
- Returns:
- its position, or -1 if not found
public ListIteratorlistIterator(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.
- Specified by:
- listIterator in interface List<E>
- Overrides:
- listIterator in interface AbstractSequentialList<E>
- Parameters:
index
- the index of the element to be returned by the first call to next(), or size() to be initially positioned at the end of the list
- Throws:
IndexOutOfBoundsException
- if index < 0 || index > size()
public boolean offer(T value)
Adds the specified element to the end of the list.
- Parameters:
value
- the value to add.
- Returns:
- true.
- Since:
- 1.5
public boolean offerFirst(T value)
Inserts the specified element at the front of the list.
- Parameters:
value
- the element to insert.
- Returns:
- true.
- Since:
- 1.6
public boolean offerLast(T value)
Inserts the specified element at the end of the list.
- Parameters:
value
- the element to insert.
- Returns:
- true.
- Since:
- 1.6
public T peek()
Returns the first element of the list without removing it.
- Returns:
- the first element of the list, or
null
if the list is empty.
- Since:
- 1.5
public T peekFirst()
Returns the first element of the list without removing it.
- Returns:
- the first element of the list, or
null
if the list is empty.
- Since:
- 1.6
public T peekLast()
Returns the last element of the list without removing it.
- Returns:
- the last element of the list, or
null
if the list is empty.
- Since:
- 1.6
public T poll()
Removes and returns the first element of the list.
- Returns:
- the first element of the list, or
null
if the list is empty.
- Since:
- 1.5
public T pollFirst()
Removes and returns the first element of the list.
- Returns:
- the first element of the list, or
null
if the list is empty.
- Since:
- 1.6
public T pollLast()
Removes and returns the last element of the list.
- Returns:
- the last element of the list, or
null
if the list is empty.
- Since:
- 1.6
public T pop()
Pops an element from the stack by removing and returning the first element in the list. This is equivalent to callingremoveFirst()
.
- Returns:
- the top of the stack, which is the first element of the list.
- Throws:
NoSuchElementException
- if the list is empty.
- Since:
- 1.6
- See Also:
removeFirst()
public void push(T value)
Pushes an element on to the stack by adding it to the front of the list. This is equivalent to callingaddFirst(T)
.
- Parameters:
value
- the element to push on to the stack.
- Throws:
NoSuchElementException
- if the list is empty.
- Since:
- 1.6
- See Also:
addFirst(T)
public T remove()
Removes and returns the first element of the list.
- Returns:
- the first element of the list.
- Throws:
NoSuchElementException
- if the list is empty.
- Since:
- 1.5
public T remove(int index)
Removes the element at the given position from the list.
- Overrides:
- remove in interface AbstractSequentialList<E>
- Parameters:
index
- the location of the element to remove
- Returns:
- the removed element
- Throws:
IndexOutOfBoundsException
- if index < 0 || index > size()
public boolean remove(Object o)
Removes the entry at the lowest index in the list that matches the given object, comparing byo == null ? e = null : o.equals(e)
.
- Specified by:
- remove in interface List<E>
- remove in interface Collection<E>
- Overrides:
- remove in interface AbstractCollection<E>
- Parameters:
o
- the object to remove
- Returns:
- true if an instance of the object was removed
public T removeFirst()
Remove and return the first element in the list.
- Returns:
- the former first element in the list
- Throws:
NoSuchElementException
- if the list is empty
public boolean removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element from the list, when traversing the list from head to tail. The list is unchanged if the element is not found. This is equivalent to callingremove(Object)
.
- Parameters:
o
- the element to remove.
- Returns:
- true if an instance of the object was removed.
- Since:
- 1.6
public T removeLast()
Remove and return the last element in the list.
- Returns:
- the former last element in the list
- Throws:
NoSuchElementException
- if the list is empty
public boolean removeLastOccurrence(Object o)
Removes the last occurrence of the specified element from the list, when traversing the list from head to tail. The list is unchanged if the element is not found.
- Parameters:
o
- the element to remove.
- Returns:
- true if an instance of the object was removed.
- Since:
- 1.6
public T set(int index, T o)
Replace the element at the given location in the list.
- Parameters:
index
- which index to changeo
- the new element
- Returns:
- the prior element
- Throws:
IndexOutOfBoundsException
- if index < 0 || index >= size()
public int size()
Returns the size of the list.
- Specified by:
- size in interface List<E>
- size in interface Collection<E>
- Overrides:
- size in interface AbstractCollection<E>
- Returns:
- the list size
public Object[] toArray()
Returns an array which contains the elements of the list in order.
- Specified by:
- toArray in interface List<E>
- toArray in interface Collection<E>
- Overrides:
- toArray in interface AbstractCollection<E>
- Returns:
- an array containing the list elements