java.util
public class LinkedList<T> extends AbstractSequentialList<T> implements List<T>, java.util.Deque<T>, 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 FormmodCount
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 ? |
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, removeRange, subList
containsAll, isEmpty, removeAll, retainAll, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
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 T getFirst()
getFirst
in interface java.util.Deque<T>
NoSuchElementException
- if the list is emptypublic T getLast()
getLast
in interface java.util.Deque<T>
NoSuchElementException
- if the list is emptypublic T removeFirst()
removeFirst
in interface java.util.Deque<T>
NoSuchElementException
- if the list is emptypublic T removeLast()
removeLast
in interface java.util.Deque<T>
NoSuchElementException
- if the list is emptypublic void addFirst(T o)
addFirst
in interface java.util.Deque<T>
o
- the element to insertpublic void addLast(T o)
addLast
in interface java.util.Deque<T>
o
- the element to insertpublic boolean contains(Object o)
o == null ? e = null : o.equals(e)
.contains
in interface Collection<T>
contains
in interface java.util.Deque<T>
contains
in interface List<T>
contains
in class AbstractCollection<T>
o
- the element to look forpublic int size()
size
in interface Collection<T>
size
in interface java.util.Deque<T>
size
in interface List<T>
size
in class AbstractCollection<T>
public boolean add(T o)
add
in interface Collection<T>
add
in interface java.util.Deque<T>
add
in interface List<T>
add
in interface java.util.Queue<T>
add
in class AbstractList<T>
o
- the entry to addAbstractList.add(int, Object)
public boolean remove(Object o)
o == null ? e = null : o.equals(e)
.remove
in interface Collection<T>
remove
in interface java.util.Deque<T>
remove
in interface List<T>
remove
in class AbstractCollection<T>
o
- the object to removeIterator.remove()
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 clear()
clear
in interface Collection<T>
clear
in interface List<T>
clear
in class AbstractList<T>
AbstractList.remove(int)
,
AbstractList.removeRange(int, int)
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 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 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 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 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 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 nullpublic T element()
element
in interface java.util.Deque<T>
element
in interface java.util.Queue<T>
NoSuchElementException
- if the list is empty.public T remove()
remove
in interface java.util.Deque<T>
remove
in interface java.util.Queue<T>
NoSuchElementException
- if the list is empty.public Iterator<T> descendingIterator()
descendingIterator
in interface java.util.Deque<T>
public boolean offerFirst(T value)
offerFirst
in interface java.util.Deque<T>
value
- the element to insert.public boolean offerLast(T value)
offerLast
in interface java.util.Deque<T>
value
- the element to insert.public T peekFirst()
peekFirst
in interface java.util.Deque<T>
null
if the list is empty.public T peekLast()
peekLast
in interface java.util.Deque<T>
null
if the list is empty.public T pollFirst()
pollFirst
in interface java.util.Deque<T>
null
if the list is empty.public T pollLast()
pollLast
in interface java.util.Deque<T>
null
if the list is empty.public T pop()
removeFirst()
.pop
in interface java.util.Deque<T>
NoSuchElementException
- if the list is empty.removeFirst()
public void push(T value)
#addFirst(T)
.push
in interface java.util.Deque<T>
value
- the element to push on to the stack.NoSuchElementException
- if the list is empty.#addFirst(T)
public boolean removeFirstOccurrence(Object o)
remove(Object)
.removeFirstOccurrence
in interface java.util.Deque<T>
o
- the element to remove.public boolean removeLastOccurrence(Object o)
removeLastOccurrence
in interface java.util.Deque<T>
o
- the element to remove.