Class LinkedList<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Deque<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.
Since
1.2
-
Field Summary
Fields inherited from class AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new empty instance ofLinkedList.LinkedList(Collection<? extends E> collection) Constructs a new instance ofLinkedListthat holds all of the elements contained in the specifiedcollection. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified object into thisLinkedListat the specified location.booleanAdds the specified object at the end of thisLinkedList.booleanaddAll(int location, Collection<? extends E> collection) Inserts the objects in the specified collection at the specified location in thisLinkedList.booleanaddAll(Collection<? extends E> collection) Adds the objects in the specified Collection to thisLinkedList.voidAdds the specified object at the beginning of thisLinkedList.voidAdds the specified object at the end of thisLinkedList.voidclear()Removes all elements from thisLinkedList, leaving it empty.booleanSearches thisLinkedListfor the specified object.Returns the iterator in reverse order, from tail to head.element()Gets but does not remove the element at the head of the queue.get(int location) Returns the element at the specified location in this list.getFirst()Returns the first element in thisLinkedList.getLast()Returns the last element in thisLinkedList.intSearches this list for the specified object and returns the index of the first occurrence.intlastIndexOf(Object object) Searches thisLinkedListfor the specified object and returns the index of the last occurrence.listIterator(int location) Returns a ListIterator on the elements of thisLinkedList.booleanInserts the specified element into the queue provided that the condition allows such an operation.booleanofferFirst(E e) Inserts an element at the head of this deque unless it would violate size limit.booleanInserts an element at the tail of this deque unless it would violate size limit.peek()Gets but does not remove the element at the head of the queue.Gets but not removes the head element of this deque.peekLast()Gets but not removes the tail element of this deque.poll()Gets and removes the element at the head of the queue, or returnsnullif there is no element in the queue.Gets and removes the head element of this deque.pollLast()Gets and removes the tail element of this deque.pop()Pops the head element of the deque, just same as removeFirst().voidPushes the element to the deque(at the head of the deque), just same as addFirst(E).remove()Gets and removes the element at the head of the queue.remove(int location) Removes the object at the specified location from thisLinkedList.booleanRemoves one instance of the specified object from thisCollectionif one is contained (optional).Removes the first object from thisLinkedList.booleanRemoves the first equivalent element of the specified object.Removes the last object from thisLinkedList.booleanRemoves the last equivalent element of the specified object.Replaces the element at the specified location in thisLinkedListwith the specified object.intsize()Returns the number of elements in thisLinkedList.Object[]toArray()Returns a new array containing all elements contained in thisLinkedList.<T> T[]toArray(T[] contents) Returns an array containing all elements contained in thisLinkedList.Methods inherited from class AbstractSequentialList
iteratorMethods inherited from class AbstractList
equals, hashCode, listIterator, removeRange, subListMethods inherited from class AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toStringMethods inherited from interface List
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, retainAll, subList
-
Constructor Details
-
LinkedList
public LinkedList()Constructs a new empty instance ofLinkedList. -
LinkedList
Constructs a new instance of
LinkedListthat holds all of the elements contained in the specifiedcollection. The order of the elements in this newLinkedListwill be determined by the iteration order ofcollection.Parameters
collection: the collection of elements to add.
-
-
Method Details
-
add
Inserts the specified object into this
LinkedListat the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of thisLinkedList, the object is added at the end.Parameters
-
location: the index at which to insert. -
object: the object to add.
Throws
IndexOutOfBoundsException: iflocation = size()
-
-
add
Adds the specified object at the end of this
LinkedList.Parameters
object: the object to add.
Returns
always true
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
addAll
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
-
location: the index at which to insert. -
collection: the collection of objects
Returns
- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractSequentialList<E>- Returns:
trueif thisLinkedListis modified,falseotherwise.Throws
-
ClassCastException: if the class of an object is inappropriate for this list. -
IllegalArgumentException: if an object cannot be added to this list. -
IndexOutOfBoundsException: iflocation size()
-
-
-
addAll
Adds the objects in the specified Collection to this
LinkedList.Parameters
collection: the collection of objects.
Returns
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Returns:
trueif thisLinkedListis modified,falseotherwise.
-
addFirst
-
addLast
-
clear
public void clear()Removes all elements from this
LinkedList, leaving it empty.See also
-
List#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
-
contains
Searches this
LinkedListfor the specified object.Parameters
object: the object to search for.
Returns
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueifobjectis an element of thisLinkedList,falseotherwise
-
get
Description copied from class:AbstractListReturns the element at the specified location in this list.
Parameters
location: the index of the element to return.
Returns
the element at the specified index.
Throws
IndexOutOfBoundsException: iflocation = size()
-
getFirst
-
getLast
-
indexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the first occurrence.
Parameters
object: the object to search for.
Returns
-
lastIndexOf
Searches this
LinkedListfor the specified object and returns the index of the last occurrence.Parameters
object: the object to search for
Returns
- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Returns:
- the index of the last occurrence of the object, or -1 if it was not found.
-
listIterator
Returns a ListIterator on the elements of this
LinkedList. The elements are iterated in the same order that they occur in theLinkedList. The iteration starts at the specified location.Parameters
location: the index at which to start the iteration
Returns
a ListIterator on the elements of this
LinkedListThrows
IndexOutOfBoundsException: iflocation = size()
See also
- ListIterator
- Specified by:
listIteratorin interfaceList<E>- Specified by:
listIteratorin classAbstractSequentialList<E>
-
remove
Removes the object at the specified location from this
LinkedList.Parameters
location: the index of the object to remove
Returns
the removed object
Throws
IndexOutOfBoundsException: iflocation = size()
-
remove
Description copied from class:AbstractCollectionRemoves one instance of the specified object from this
Collectionif one is contained (optional). This implementation iterates over thisCollectionand tests for each elementereturned by the iterator, whethereis equal to the given object. Ifobject != nullthen this test is performed usingobject.equals(e), otherwise usingobject == null. If an element equal to the given object is found, then theremovemethod is called on the iterator andtrueis returned,falseotherwise. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.Parameters
object: the object to remove.
Returns
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: if the object passed is not of the correct type. -
NullPointerException: @throws NullPointerException ifobjectisnulland thisCollectiondoesn't supportnullelements.
-
-
removeFirst
Removes the first object from this
LinkedList.Returns
the removed object.
Throws
NoSuchElementException: if thisLinkedListis empty.
- Specified by:
removeFirstin interfaceDeque<E>
-
removeLast
Removes the last object from this
LinkedList.Returns
the removed object.
Throws
NoSuchElementException: if thisLinkedListis empty.
- Specified by:
removeLastin interfaceDeque<E>
-
descendingIterator
Returns the iterator in reverse order, from tail to head.
Returns
the iterator in reverse order
Since
1.6
See also
- java.util.Deque#descendingIterator()
- Specified by:
descendingIteratorin interfaceDeque<E>
-
offerFirst
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
e: 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: @throws 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.
Since
1.6
See also
- java.util.Deque#offerFirst(java.lang.Object)
- Specified by:
offerFirstin interfaceDeque<E>
-
offerLast
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
e: 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: @throws 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
Since
1.6
See also
- java.util.Deque#offerLast(java.lang.Object)
-
peekFirst
-
peekLast
-
pollFirst
-
pollLast
-
pop
-
push
Pushes the element to the deque(at the head of the deque), just same as addFirst(E).
Parameters
e: 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: @throws 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.
Since
1.6
See also
- java.util.Deque#push(java.lang.Object)
-
removeFirstOccurrence
Removes the first equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.
Parameters
o: the element to be removed
Returns
Since
1.6
See also
- java.util.Deque#removeFirstOccurrence(java.lang.Object)
- Specified by:
removeFirstOccurrencein interfaceDeque<E>- 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: @throws NullPointerException if the element is null and the deque can not contain null element
-
-
removeLastOccurrence
Removes the last equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.
Parameters
o: the element to be removed
Returns
Since
1.6
See also
- java.util.Deque#removeLastOccurrence(java.lang.Object)
- Specified by:
removeLastOccurrencein interfaceDeque<E>- 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: @throws NullPointerException if the element is null and the deque can not contain null element
-
-
set
Replaces the element at the specified location in this
LinkedListwith the specified object.Parameters
-
location: the index at which to put the specified object. -
object: 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: iflocation = size()
-
-
size
public int size()Returns the number of elements in this
LinkedList.Returns
the number of elements in this
LinkedList.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
offer
Description copied from interface:QueueInserts 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
o: the specified element to insert into the queue.
Returns
-
poll
Description copied from interface:QueueGets and removes the element at the head of the queue, or returns
nullif there is no element in the queue.Returns
-
remove
Description copied from interface:QueueGets 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
-
element
Description copied from interface:QueueGets but does not remove the element at the head of the queue. Throws a
NoSuchElementExceptionif 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
Returns a new array containing all elements contained in this
LinkedList.Returns
an array of the elements from this
LinkedList.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>
-
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 thisLinkedList, the array element following the collection elements is set to null.Parameters
contents: the array.
Returns
an array of the elements from this
LinkedList.Throws
ArrayStoreException: @throws ArrayStoreException if the type of an element in thisLinkedListcannot be stored in the type of the specified array.
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>
-