public class LinkedList<E>
ImplementsCollection<E>, Deque<E>, Iterable<E>, List<E>, Queue<E>
LinkedList is an implementation of List, backed by a linked list. All
optional operations are supported, adding, removing and replacing. The
elements can be any objects.
Constructors
public LinkedList() | Constructs a new empty instance of LinkedList. |
public LinkedList(Collection<? extends E> collection) | Constructs a new instance of LinkedList that holds all of the elements contained in the specified collection. |
Methods
public void add(int location, E object) | Inserts the specified object into this LinkedList at the specified location. |
public boolean add(E object) | Adds the specified object at the end of this LinkedList. |
public boolean addAll(int location, Collection<? extends E> collection) | Inserts the objects in the specified collection at the specified location in this LinkedList. |
public boolean addAll(Collection<? extends E> collection) | Adds the objects in the specified Collection to this LinkedList. |
public void addFirst(E object) | Adds the specified object at the beginning of this LinkedList. |
public void addLast(E object) | Adds the specified object at the end of this LinkedList. |
public void clear() | Removes all elements from this LinkedList, leaving it empty. |
public boolean contains(Object object) | Searches this LinkedList for the specified object. |
public E get(int location) | Returns the element at the specified location in this list. |
public E getFirst() | Returns the first element in this LinkedList. |
public E getLast() | Returns the last element in this LinkedList. |
public int indexOf(Object object) | Searches this list for the specified object and returns the index of the first occurrence. |
public int lastIndexOf(Object object) | Searches this LinkedList for the specified object and returns the index of the last occurrence. |
public ListIterator<E> listIterator(int location) | Returns a ListIterator on the elements of this LinkedList. |
public E remove(int location) | Removes the object at the specified location from this LinkedList. |
public boolean remove(Object object) | Removes one instance of the specified object from this Collection if one is contained (optional). |
public E removeFirst() | Removes the first object from this LinkedList. |
public E removeLast() | Removes the last object from this LinkedList. |
public Iterator<E> descendingIterator() | Returns the iterator in reverse order, from tail to head. |
public boolean offerFirst(E e) | Inserts an element at the head of this deque unless it would violate size limit. |
public boolean offerLast(E e) | Inserts an element at the tail of this deque unless it would violate size limit. |
public E peekFirst() | Gets but not removes the head element of this deque. |
public E peekLast() | Gets but not removes the tail element of this deque. |
public E pollFirst() | Gets and removes the head element of this deque. |
public E pollLast() | Gets and removes the tail element of this deque. |
public E pop() | Pops the head element of the deque, just same as removeFirst(). |
public void push(E e) | Pushes the element to the deque(at the head of the deque), just same as addFirst(E). |
public boolean removeFirstOccurrence(Object o) | Removes the first equivalent element of the specified object. |
public boolean removeLastOccurrence(Object o) | Removes the last equivalent element of the specified object. |
public E set(int location, E object) | Replaces the element at the specified location in this LinkedList with the specified object. |
public int size() | Returns the number of elements in this LinkedList. |
public boolean offer(E o) | Inserts the specified element into the queue provided that the condition allows such an operation. |
public E poll() | Gets and removes the element at the head of the queue, or returns null if there is no element in the queue. |
public E remove() | Gets and removes the element at the head of the queue. |
public E peek() | Gets but does not remove the element at the head of the queue. |
public E element() | Gets but does not remove the element at the head of the queue. |
public Object[] toArray() | Returns a new array containing all elements contained in this LinkedList. |
public <T> T[] toArray(T[] contents) | Returns an array containing all elements contained in this LinkedList. |
Inherited fields
From AbstractList
Inherited methods
From Collection
From List
Constructor details
LinkedList
public LinkedList()Constructs a new empty instance of
LinkedList.LinkedList
public LinkedList(Collection<? extends E> collection)Constructs a new instance of
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.Parameters
collectionCollection<? extends E>- the collection of elements to add.
Method details
add
public void add(int location, E object)Inserts the specified object into this
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.Parameters
locationint- the index at which to insert.
objectE- the object to add.
Throws
IndexOutOfBoundsException- if
location = size() UnsupportedOperationException- if adding to this List is not supported.
ClassCastException- if the class of the object is inappropriate for this List
IllegalArgumentException- if the object cannot be added to this List
add
public boolean add(E object)Adds the specified object at the end of this
LinkedList.Parameters
objectE- the object to add.
Returns
always true
addAll
public boolean addAll(int location, Collection<? extends E> collection)Inserts the objects in the specified collection at the specified location
in this
LinkedList. The objects are added in the order they are
returned from the collection’s iterator.Parameters
locationint- the index at which to insert.
collectionCollection<? extends E>- the collection of objects
Returns
true if this LinkedList is modified,
false otherwise.Throws
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 size()
addAll
public boolean addAll(Collection<? extends E> collection)Adds the objects in the specified Collection to this
LinkedList.Parameters
collectionCollection<? extends E>- the collection of objects.
Returns
true if this LinkedList is modified,
false otherwise.addFirst
public void addFirst(E object)Adds the specified object at the beginning of this
LinkedList.Parameters
objectE- the object to add.
Throws
IllegalStateException- if it can not add now due to size limit
ClassCastException- if the class of element can not be added into this deque
NullPointerException- if the element is null and the deque can not contain null element
IllegalArgumentException- if the element can not be added due to some property.
addLast
public void addLast(E object)Adds the specified object at the end of this
LinkedList.Parameters
objectE- the object to add.
Throws
IllegalStateException- if it can not add now due to size limit
ClassCastException- if the class of element can not be added into this deque
NullPointerException- if the element is null and the deque can not contain null element
IllegalArgumentException- if the element can not be added due to some property.
clear
public void clear()Removes all elements from this
LinkedList, leaving it empty.Throws
UnsupportedOperationException- if removing from this list is not supported.
See also
contains
public boolean contains(Object object)Searches this
LinkedList for the specified object.Parameters
objectObject- the object to search for.
Returns
true if object is an element of this
LinkedList, false otherwiseget
public E get(int location)Returns the element at the specified location in this list.
Parameters
locationint- the index of the element to return.
Returns
the element at the specified index.
Throws
IndexOutOfBoundsException- if
location = size()
getFirst
public E getFirst()Returns the first element in this
LinkedList.Returns
the first element.
Throws
NoSuchElementException- if this
LinkedListis empty.
getLast
public E getLast()Returns the last element in this
LinkedList.Returns
the last element
Throws
NoSuchElementException- if this
LinkedListis empty
indexOf
public int indexOf(Object object)Searches this list for the specified object and returns the index of the
first occurrence.
Parameters
objectObject- the object to search for.
Returns
the index of the first occurrence of the object, or -1 if it was
not found.
lastIndexOf
public int lastIndexOf(Object object)Searches this
LinkedList for the specified object and returns the
index of the last occurrence.Parameters
objectObject- the object to search for
Returns
the index of the last occurrence of the object, or -1 if it was
not found.
listIterator
public ListIterator<E> listIterator(int location)Returns a ListIterator on the elements of this
LinkedList. The
elements are iterated in the same order that they occur in the
LinkedList. The iteration starts at the specified location.Parameters
locationint- the index at which to start the iteration
Returns
a ListIterator on the elements of this
LinkedListThrows
IndexOutOfBoundsException- if
location = size()
See also
remove
public E remove(int location)Removes the object at the specified location from this
LinkedList.Parameters
locationint- the index of the object to remove
Returns
the removed object
Throws
IndexOutOfBoundsException- if
location = size()
remove
public boolean remove(Object object)Removes one instance of the specified object from this
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.Parameters
objectObject- the object to remove.
Returns
true if this Collection is modified, false
otherwise.Throws
UnsupportedOperationException- if removing from this
Collectionis not supported. ClassCastException- if the object passed is not of the correct type.
NullPointerException- if
objectisnulland thisCollectiondoesn’t supportnullelements.
removeFirst
public E removeFirst()Removes the first object from this
LinkedList.Returns
the removed object.
Throws
NoSuchElementException- if this
LinkedListis empty.
removeLast
public E removeLast()Removes the last object from this
LinkedList.Returns
the removed object.
Throws
NoSuchElementException- if this
LinkedListis empty.
descendingIterator
public Iterator<E> descendingIterator()Returns the iterator in reverse order, from tail to head.
Returns
the iterator in reverse order
offerFirst
public boolean offerFirst(E e)Inserts an element at the head of this deque unless it would violate size
limit. It is better than the addFirst(E) method in a size-limited deque,
because the latter one may fail to add the element only by throwing an
exception.
Parameters
eE- the element
Returns
true if the operation succeeds or false if it fails.
Throws
ClassCastException- if the class of element can not be added into this deque
NullPointerException- if the element is null and the deque can not contain null element
IllegalArgumentException- if the element can not be added due to some property.
offerLast
public boolean offerLast(E e)Inserts an element at the tail of this deque unless it would violate size
limit. It is better than the addLast(E) method in a size-limited deque,
because the latter one may fail to add the element only by throwing an
exception.
Parameters
eE- the element
Returns
true if the operation succeeds or false if it fails
Throws
ClassCastException- if the class of element can not be added into this deque
NullPointerException- if the element is null and the deque can not contain null element
IllegalArgumentException- if the element can not be added due to some property
peekFirst
public E peekFirst()Gets but not removes the head element of this deque. This method returns
null if the deque is empty.
Returns
the head element or null if the deque is empty
See also
peekLast
public E peekLast()Gets but not removes the tail element of this deque. This method returns
null if the deque is empty.
Returns
the tail element or null if the deque is empty
See also
pollFirst
public E pollFirst()Gets and removes the head element of this deque. This method returns null
if the deque is empty.
Returns
the head element or null if the deque is empty
See also
pollLast
public E pollLast()Gets and removes the tail element of this deque. This method returns null
if the deque is empty.
Returns
the tail element or null if the deque is empty
See also
pop
public E pop()Pops the head element of the deque, just same as removeFirst().
Returns
the head element
Throws
NoSuchElementException- if the deque is empty
See also
push
public void push(E e)Pushes the element to the deque(at the head of the deque), just same as
addFirst(E).
Parameters
eE- the element
Throws
IllegalStateException- if it can not add now due to size limit
ClassCastException- if the class of element can not be added into this deque
NullPointerException- if the element is null and the deque can not contain null element
IllegalArgumentException- if the element can not be added due to some property.
removeFirstOccurrence
public boolean removeFirstOccurrence(Object o)Removes the first equivalent element of the specified object. If the
deque does not contain the element, it is unchanged and returns false.
Parameters
oObject- the element to be removed
Returns
true if the operation succeeds or false if the deque does not
contain the element.
Throws
ClassCastException- if the class of the element is incompatible with the deque
NullPointerException- if the element is null and the deque can not contain null element
removeLastOccurrence
public boolean removeLastOccurrence(Object o)Removes the last equivalent element of the specified object. If the deque
does not contain the element, it is unchanged and returns false.
Parameters
oObject- the element to be removed
Returns
true if the operation succeeds or false if the deque does not
contain the element.
Throws
ClassCastException- if the class of the element is incompatible with the deque
NullPointerException- if the element is null and the deque can not contain null element
set
public E set(int location, E object)Replaces the element at the specified location in this
LinkedList
with the specified object.Parameters
locationint- the index at which to put the specified object.
objectE- the object to add.
Returns
the previous element at the index.
Throws
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 = size()
size
public int size()Returns the number of elements in this
LinkedList.Returns
the number of elements in this
LinkedList.offer
public boolean offer(E o)Inserts the specified element into the queue provided that the condition
allows such an operation. The method is generally preferable to
Collection#add, since the latter might throw an exception if the
operation fails.Parameters
oE- the specified element to insert into the queue.
Returns
true if the operation succeeds and false if it
fails.poll
public E poll()Gets and removes the element at the head of the queue, or returns
null if there is no element in the queue.Returns
the element at the head of the queue or
null if there is
no element in the queue.remove
public E remove()Gets and removes the element at the head of the queue. Throws a
NoSuchElementException if there is no element in the queue.
Returns
the element at the head of the queue.
Throws
NoSuchElementException- if there is no element in the queue.
peek
public E peek()Gets but does not remove the element at the head of the queue.
Returns
the element at the head of the queue or
null if there is
no element in the queue.element
public E element()Gets but does not remove the element at the head of the queue. Throws a
NoSuchElementException if there is no element in the queue.Returns
the element at the head of the queue.
Throws
NoSuchElementException- if there is no element in the queue.
toArray
public Object[] toArray()Returns a new array containing all elements contained in this
LinkedList.Returns
an array of the elements from this
LinkedList.toArray
public <T> T[] toArray(T[] contents)Returns an array containing all elements contained in this
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.Parameters
contentsT[]- the array.
Returns
an array of the elements from this
LinkedList.Throws
ArrayStoreException- if the type of an element in this
LinkedListcannot be stored in the type of the specified array.