public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>
modCount
Constructor and Description |
---|
LinkedList()
Constructs a new empty instance of
LinkedList . |
LinkedList(Collection<? extends E> collection)
Constructs a new instance of
LinkedList that holds all of the
elements contained in the specified collection . |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds the specified object at the end of this
LinkedList . |
void |
add(int location,
E object)
Inserts the specified object into this
LinkedList at the
specified location. |
boolean |
addAll(Collection<? extends E> collection)
Adds the objects in the specified Collection to this
LinkedList . |
boolean |
addAll(int location,
Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location
in this
LinkedList . |
void |
addFirst(E object)
Adds the specified object at the beginning of this
LinkedList . |
void |
addLast(E object)
Adds the specified object at the end of this
LinkedList . |
void |
clear()
Removes all elements from this
LinkedList , leaving it empty. |
boolean |
contains(Object object)
Searches this
LinkedList for the specified object. |
Iterator<E> |
descendingIterator()
Returns the iterator in reverse order, from tail to head.
|
E |
element()
Gets but does not remove the element at the head of the queue.
|
E |
get(int location)
Returns the element at the specified location in this list.
|
E |
getFirst()
Returns the first element in this
LinkedList . |
E |
getLast()
Returns the last element in this
LinkedList . |
int |
indexOf(Object object)
Searches this list for the specified object and returns the index of the
first occurrence.
|
int |
lastIndexOf(Object object)
Searches this
LinkedList for the specified object and returns the
index of the last occurrence. |
ListIterator<E> |
listIterator(int location)
Returns a ListIterator on the elements of this
LinkedList . |
boolean |
offer(E o)
Inserts the specified element into the queue provided that the condition
allows such an operation.
|
boolean |
offerFirst(E e)
Inserts an element at the head of this deque unless it would violate size
limit.
|
boolean |
offerLast(E e)
Inserts an element at the tail of this deque unless it would violate size
limit.
|
E |
peek()
Gets but does not remove the element at the head of the queue.
|
E |
peekFirst()
Gets but not removes the head element of this deque.
|
E |
peekLast()
Gets but not removes the tail element of this deque.
|
E |
poll()
Gets and removes the element at the head of the queue, or returns
null if there is no element in the queue. |
E |
pollFirst()
Gets and removes the head element of this deque.
|
E |
pollLast()
Gets and removes the tail element of this deque.
|
E |
pop()
Pops the head element of the deque, just same as removeFirst().
|
void |
push(E e)
Pushes the element to the deque(at the head of the deque), just same as
addFirst(E).
|
E |
remove()
Gets and removes the element at the head of the queue.
|
E |
remove(int location)
Removes the object at the specified location from this
LinkedList . |
boolean |
remove(Object object)
Removes one instance of the specified object from this
Collection if one
is contained (optional). |
E |
removeFirst()
Removes the first object from this
LinkedList . |
boolean |
removeFirstOccurrence(Object o)
Removes the first equivalent element of the specified object.
|
E |
removeLast()
Removes the last object from this
LinkedList . |
boolean |
removeLastOccurrence(Object o)
Removes the last equivalent element of the specified object.
|
E |
set(int location,
E object)
Replaces the element at the specified location in this
LinkedList
with the specified object. |
int |
size()
Returns the number of elements in this
LinkedList . |
Object[] |
toArray()
Returns a new array containing all elements contained in this
LinkedList . |
<T> T[] |
toArray(T[] contents)
Returns an array containing all elements contained in this
LinkedList . |
iterator
equals, hashCode, listIterator, removeRange, subList
containsAll, isEmpty, removeAll, retainAll, toString
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, retainAll, subList
public LinkedList()
LinkedList
.public LinkedList(Collection<? extends E> collection)
LinkedList
that holds all of the
elements contained in the specified collection
. The order of the
elements in this new LinkedList
will be determined by the
iteration order of collection
.collection
- the collection of elements to add.public void add(int location, E object)
LinkedList
at the
specified location. The object is inserted before any previous element at
the specified location. If the location is equal to the size of this
LinkedList
, the object is added at the end.add
in interface List<E>
add
in class AbstractSequentialList<E>
location
- the index at which to insert.object
- the object to add.IndexOutOfBoundsException
- if location < 0 || >= size()
public boolean add(E object)
LinkedList
.add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
object
- the object to add.public boolean addAll(int location, Collection<? extends E> collection)
LinkedList
. The objects are added in the order they are
returned from the collection's iterator.addAll
in interface List<E>
addAll
in class AbstractSequentialList<E>
location
- the index at which to insert.collection
- the collection of objectstrue
if this LinkedList
is modified,
false
otherwise.ClassCastException
- if the class of an object is inappropriate for this list.IllegalArgumentException
- if an object cannot be added to this list.IndexOutOfBoundsException
- if location < 0 || > size()
public boolean addAll(Collection<? extends E> collection)
LinkedList
.addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class AbstractCollection<E>
collection
- the collection of objects.true
if this LinkedList
is modified,
false
otherwise.public void addFirst(E object)
LinkedList
.public void addLast(E object)
LinkedList
.public void clear()
LinkedList
, leaving it empty.clear
in interface Collection<E>
clear
in interface List<E>
clear
in class AbstractList<E>
List.isEmpty()
,
size
public boolean contains(Object object)
LinkedList
for the specified object.contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
object
- the object to search for.true
if object
is an element of this
LinkedList
, false
otherwisepublic E get(int location)
AbstractList
public E getFirst()
LinkedList
.getFirst
in interface Deque<E>
NoSuchElementException
- if this LinkedList
is empty.public E getLast()
LinkedList
.getLast
in interface Deque<E>
NoSuchElementException
- if this LinkedList
is emptypublic int indexOf(Object object)
AbstractList
public int lastIndexOf(Object object)
LinkedList
for the specified object and returns the
index of the last occurrence.lastIndexOf
in interface List<E>
lastIndexOf
in class AbstractList<E>
object
- the object to search forpublic ListIterator<E> listIterator(int location)
LinkedList
. The
elements are iterated in the same order that they occur in the
LinkedList
. The iteration starts at the specified location.listIterator
in interface List<E>
listIterator
in class AbstractSequentialList<E>
location
- the index at which to start the iterationLinkedList
IndexOutOfBoundsException
- if location < 0 || >= size()
ListIterator
public E remove(int location)
LinkedList
.remove
in interface List<E>
remove
in class AbstractSequentialList<E>
location
- the index of the object to removeIndexOutOfBoundsException
- if location < 0 || >= size()
public boolean remove(Object object)
AbstractCollection
Collection
if one
is contained (optional). This implementation iterates over this
Collection
and tests for each element e
returned by the iterator,
whether e
is equal to the given object. If object != null
then this test is performed using object.equals(e)
, otherwise
using object == null
. If an element equal to the given object is
found, then the remove
method is called on the iterator and
true
is returned, false
otherwise. If the iterator does
not support removing elements, an UnsupportedOperationException
is thrown.remove
in interface Collection<E>
remove
in interface List<E>
remove
in class AbstractCollection<E>
object
- the object to remove.true
if this Collection
is modified, false
otherwise.public E removeFirst()
LinkedList
.removeFirst
in interface Deque<E>
NoSuchElementException
- if this LinkedList
is empty.public E removeLast()
LinkedList
.removeLast
in interface Deque<E>
NoSuchElementException
- if this LinkedList
is empty.public Iterator<E> descendingIterator()
descendingIterator
in interface Deque<E>
Deque.descendingIterator()
public boolean offerFirst(E e)
offerFirst
in interface Deque<E>
e
- the elementDeque.offerFirst(java.lang.Object)
public boolean offerLast(E e)
offerLast
in interface Deque<E>
e
- the elementDeque.offerLast(java.lang.Object)
public E peekFirst()
peekFirst
in interface Deque<E>
Deque.peekFirst()
public E peekLast()
peekLast
in interface Deque<E>
Deque.peekLast()
public E pollFirst()
pollFirst
in interface Deque<E>
Deque.pollFirst()
public E pollLast()
pollLast
in interface Deque<E>
Deque.pollLast()
public E pop()
pop
in interface Deque<E>
Deque.pop()
public void push(E e)
push
in interface Deque<E>
e
- the elementDeque.push(java.lang.Object)
public boolean removeFirstOccurrence(Object o)
removeFirstOccurrence
in interface Deque<E>
o
- the element to be removedDeque.removeFirstOccurrence(java.lang.Object)
public boolean removeLastOccurrence(Object o)
removeLastOccurrence
in interface Deque<E>
o
- the element to be removedDeque.removeLastOccurrence(java.lang.Object)
public E set(int location, E object)
LinkedList
with the specified object.set
in interface List<E>
set
in class AbstractSequentialList<E>
location
- the index at which to put the specified object.object
- the object to add.ClassCastException
- if the class of an object is inappropriate for this list.IllegalArgumentException
- if an object cannot be added to this list.IndexOutOfBoundsException
- if location < 0 || >= size()
public int size()
LinkedList
.size
in interface Collection<E>
size
in interface List<E>
size
in class AbstractCollection<E>
LinkedList
.public boolean offer(E o)
Queue
Collection.add(E)
, since the latter might throw an exception if the
operation fails.public E poll()
Queue
null
if there is no element in the queue.public E remove()
Queue
public E peek()
Queue
public E element()
Queue
NoSuchElementException
if there is no element in the queue.public Object[] toArray()
LinkedList
.toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractList<E>
LinkedList
.public <T> T[] toArray(T[] contents)
LinkedList
. If the specified array is large enough to hold the
elements, the specified array is used, otherwise an array of the same
type is created. If the specified array is used and is larger than this
LinkedList
, the array element following the collection elements
is set to null.toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractList<E>
contents
- the array.LinkedList
.ArrayStoreException
- if the type of an element in this LinkedList
cannot
be stored in the type of the specified array.