public class LinkedList<T> extends AbstractSequentialList<T> implements List<T>, Deque<T>, Cloneable
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)
Constructor and Description |
---|
LinkedList()
Create an empty linked list.
|
LinkedList(Collection<? extends T> c)
Create a linked list containing the elements, in order, of a given
collection.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
T o)
Inserts an element in the given position in the list.
|
boolean |
add(T o)
Adds an element to the end of the list.
|
boolean |
addAll(Collection<? extends T> c)
Append the elements of the collection in iteration order to the end of
this list.
|
boolean |
addAll(int index,
Collection<? extends T> c)
Insert the elements of the collection in iteration order at the given
index of this list.
|
void |
addFirst(T o)
Insert an element at the first of the list.
|
void |
addLast(T o)
Insert an element at the last of the list.
|
void |
clear()
Remove all elements from this list.
|
Object |
clone()
Create a shallow copy of this LinkedList (the elements are not cloned).
|
boolean |
contains(Object o)
Returns true if the list contains the given object.
|
Iterator<T> |
descendingIterator()
Obtain an Iterator over this list in reverse sequential order.
|
T |
element()
Returns the first element of the list without removing
it.
|
T |
get(int index)
Return the element at index.
|
T |
getFirst()
Returns the first element in the list.
|
T |
getLast()
Returns the last element in the list.
|
int |
indexOf(Object o)
Returns the first index where the element is located in the list, or -1.
|
int |
lastIndexOf(Object o)
Returns the last index where the element is located in the list, or -1.
|
ListIterator<T> |
listIterator(int index)
Obtain a ListIterator over this list, starting at a given index.
|
boolean |
offer(T value)
Adds the specified element to the end of the list.
|
boolean |
offerFirst(T value)
Inserts the specified element at the front of the list.
|
boolean |
offerLast(T value)
Inserts the specified element at the end of the list.
|
T |
peek()
Returns the first element of the list without removing
it.
|
T |
peekFirst()
Returns the first element of the list without removing
it.
|
T |
peekLast()
Returns the last element of the list without removing
it.
|
T |
poll()
Removes and returns the first element of the list.
|
T |
pollFirst()
Removes and returns the first element of the list.
|
T |
pollLast()
Removes and returns the last element of the list.
|
T |
pop()
Pops an element from the stack by removing and returning
the first element in the list.
|
void |
push(T value)
Pushes an element on to the stack by adding it to the
front of the list.
|
T |
remove()
Removes and returns the first element of the list.
|
T |
remove(int index)
Removes the element at the given position from the list.
|
boolean |
remove(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) . |
T |
removeFirst()
Remove and return the first element in the list.
|
boolean |
removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element
from the list, when traversing the list from head to
tail.
|
T |
removeLast()
Remove and return the last element in the list.
|
boolean |
removeLastOccurrence(Object o)
Removes the last occurrence of the specified element
from the list, when traversing the list from head to
tail.
|
T |
set(int index,
T o)
Replace the element at the given location in the list.
|
int |
size()
Returns the size of the list.
|
Object[] |
toArray()
Returns an array which contains the elements of the list in order.
|
<S> S[] |
toArray(S[] a)
Returns an Array whose component type is the runtime component type of
the passed-in Array.
|
iterator
equals, hashCode, listIterator, subList
containsAll, isEmpty, removeAll, retainAll, toString
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, retainAll, subList
public LinkedList()
public LinkedList(Collection<? extends T> c)
c
- the collection to populate this list fromNullPointerException
- if c is nullpublic void add(int index, T o)
add
in interface List<T>
add
in class AbstractSequentialList<T>
index
- where to insert the elemento
- the element to insertIndexOutOfBoundsException
- if index < 0 || index > size()AbstractList.modCount
public boolean add(T o)
add
in interface Collection<T>
add
in interface Deque<T>
add
in interface List<T>
add
in interface Queue<T>
add
in class AbstractList<T>
o
- the entry to addAbstractList.add(int, Object)
public boolean addAll(Collection<? extends T> c)
addAll
in interface Collection<T>
addAll
in interface List<T>
addAll
in class AbstractCollection<T>
c
- the collection to appendNullPointerException
- if c is nullAbstractCollection.add(Object)
public boolean addAll(int index, Collection<? extends T> c)
addAll
in interface List<T>
addAll
in class AbstractSequentialList<T>
c
- the collection to appendindex
- the location to insert the collectionNullPointerException
- if c is nullIndexOutOfBoundsException
- if index < 0 || index > size()AbstractSequentialList.add(int, Object)
public void addFirst(T o)
public void addLast(T o)
public void clear()
clear
in interface Collection<T>
clear
in interface List<T>
clear
in class AbstractList<T>
AbstractList.remove(int)
,
AbstractList.removeRange(int, int)
public Object clone()
public boolean contains(Object o)
o == null ? e = null : o.equals(e)
.public Iterator<T> descendingIterator()
descendingIterator
in interface Deque<T>
public T element()
public T get(int index)
get
in interface List<T>
get
in class AbstractSequentialList<T>
index
- the place to lookIndexOutOfBoundsException
- if index < 0 || index >= size()public T getFirst()
getFirst
in interface Deque<T>
NoSuchElementException
- if the list is emptypublic T getLast()
getLast
in interface Deque<T>
NoSuchElementException
- if the list is emptypublic int indexOf(Object o)
public int lastIndexOf(Object o)
lastIndexOf
in interface List<T>
lastIndexOf
in class AbstractList<T>
o
- the element to look forpublic ListIterator<T> listIterator(int index)
listIterator
in interface List<T>
listIterator
in class AbstractSequentialList<T>
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 listIndexOutOfBoundsException
- if index < 0 || index > size()AbstractList.modCount
public boolean offer(T value)
public boolean offerFirst(T value)
offerFirst
in interface Deque<T>
value
- the element to insert.public boolean offerLast(T value)
public T peek()
public T peekFirst()
public T peekLast()
public T poll()
public T pollFirst()
public T pollLast()
public T pop()
removeFirst()
.pop
in interface Deque<T>
NoSuchElementException
- if the list is empty.removeFirst()
public void push(T value)
#addFirst(T)
.push
in interface Deque<T>
value
- the element to push on to the stack.NoSuchElementException
- if the list is empty.#addFirst(T)
public T remove()
public T remove(int index)
remove
in interface List<T>
remove
in class AbstractSequentialList<T>
index
- the location of the element to removeIndexOutOfBoundsException
- if index < 0 || index > size()AbstractList.modCount
public boolean remove(Object o)
o == null ? e = null : o.equals(e)
.remove
in interface Collection<T>
remove
in interface Deque<T>
remove
in interface List<T>
remove
in class AbstractCollection<T>
o
- the object to removeIterator.remove()
public T removeFirst()
removeFirst
in interface Deque<T>
NoSuchElementException
- if the list is emptypublic boolean removeFirstOccurrence(Object o)
remove(Object)
.removeFirstOccurrence
in interface Deque<T>
o
- the element to remove.public T removeLast()
removeLast
in interface Deque<T>
NoSuchElementException
- if the list is emptypublic boolean removeLastOccurrence(Object o)
removeLastOccurrence
in interface Deque<T>
o
- the element to remove.public T set(int index, T o)
set
in interface List<T>
set
in class AbstractSequentialList<T>
index
- which index to changeo
- the new elementIndexOutOfBoundsException
- if index < 0 || index >= size()public int size()
public Object[] toArray()
toArray
in interface Collection<T>
toArray
in interface List<T>
toArray
in class AbstractCollection<T>
public <S> S[] toArray(S[] a)
toArray
in interface Collection<T>
toArray
in interface List<T>
toArray
in class AbstractCollection<T>
a
- the passed-in ArrayArrayStoreException
- if the runtime type of a does not allow
an element in this listNullPointerException
- if a is null