Class LinkedList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.AbstractSequentialList<E>
java.util.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 ofLinkedListthat 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 thisLinkedListat 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.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractSequentialList<E>- Parameters:
location- the index at which to insert.object- the object to add.- Throws:
IndexOutOfBoundsException- iflocation < 0 || >= size()
-
add
Adds the specified object at the end of thisLinkedList.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Parameters:
object- the object to add.- Returns:
- always true
-
addAll
Inserts the objects in the specified collection at the specified location in thisLinkedList. The objects are added in the order they are returned from the collection's iterator.- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractSequentialList<E>- Parameters:
location- the index at which to insert.collection- the collection of objects- 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 < 0 || > size()
-
addAll
Adds the objects in the specified Collection to thisLinkedList.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif thisLinkedListis modified,falseotherwise.
-
addFirst
-
addLast
-
clear
public void clear()Removes all elements from thisLinkedList, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>- See Also:
-
contains
Searches thisLinkedListfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisLinkedList,falseotherwise
-
get
Description copied from class:AbstractListReturns the element at the specified location in this list. -
getFirst
Returns the first element in thisLinkedList.- Specified by:
getFirstin interfaceDeque<E>- Returns:
- the first element.
- Throws:
NoSuchElementException- if thisLinkedListis empty.
-
getLast
Returns the last element in thisLinkedList.- Specified by:
getLastin interfaceDeque<E>- Returns:
- the last element
- Throws:
NoSuchElementException- if thisLinkedListis empty
-
indexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the first occurrence. -
lastIndexOf
Searches thisLinkedListfor the specified object and returns the index of the last occurrence.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Parameters:
object- the object to search for- 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 thisLinkedList. The elements are iterated in the same order that they occur in theLinkedList. The iteration starts at the specified location.- Specified by:
listIteratorin interfaceList<E>- Specified by:
listIteratorin classAbstractSequentialList<E>- Parameters:
location- the index at which to start the iteration- Returns:
- a ListIterator on the elements of this
LinkedList - Throws:
IndexOutOfBoundsException- iflocation < 0 || >= size()- See Also:
-
remove
Removes the object at the specified location from thisLinkedList.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractSequentialList<E>- Parameters:
location- the index of the object to remove- Returns:
- the removed object
- Throws:
IndexOutOfBoundsException- iflocation < 0 || >= size()
-
remove
Description copied from class:AbstractCollectionRemoves one instance of the specified object from thisCollectionif 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.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
object- the object to remove.- Returns:
trueif thisCollectionis modified,falseotherwise.
-
removeFirst
Removes the first object from thisLinkedList.- Specified by:
removeFirstin interfaceDeque<E>- Returns:
- the removed object.
- Throws:
NoSuchElementException- if thisLinkedListis empty.
-
removeLast
Removes the last object from thisLinkedList.- Specified by:
removeLastin interfaceDeque<E>- Returns:
- the removed object.
- Throws:
NoSuchElementException- if thisLinkedListis empty.
-
descendingIterator
Returns the iterator in reverse order, from tail to head.- Specified by:
descendingIteratorin interfaceDeque<E>- Returns:
- the iterator in reverse order
- Since:
- 1.6
- See Also:
-
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.- Specified by:
offerFirstin interfaceDeque<E>- Parameters:
e- the element- Returns:
- true if the operation succeeds or false if it fails.
- Since:
- 1.6
- See Also:
-
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. -
peekFirst
-
peekLast
-
pollFirst
-
pollLast
-
pop
-
push
-
removeFirstOccurrence
Removes the first equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.- Specified by:
removeFirstOccurrencein interfaceDeque<E>- Parameters:
o- the element to be removed- Returns:
- true if the operation succeeds or false if the deque does not contain the element.
- Since:
- 1.6
- See Also:
-
removeLastOccurrence
Removes the last equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.- Specified by:
removeLastOccurrencein interfaceDeque<E>- Parameters:
o- the element to be removed- Returns:
- true if the operation succeeds or false if the deque does not contain the element.
- Since:
- 1.6
- See Also:
-
set
Replaces the element at the specified location in thisLinkedListwith the specified object.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractSequentialList<E>- 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 < 0 || >= size()
-
size
public int size()Returns the number of elements in thisLinkedList.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this
LinkedList.
-
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 toCollection.add(E), since the latter might throw an exception if the operation fails. -
poll
-
remove
-
peek
-
element
-
toArray
Returns a new array containing all elements contained in thisLinkedList.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>- Returns:
- an array of the elements from this
LinkedList.
-
toArray
public <T> T[] toArray(T[] contents) Returns an array containing all elements contained in thisLinkedList. 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.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>- Parameters:
contents- the array.- Returns:
- an array of the elements from this
LinkedList. - Throws:
ArrayStoreException- if the type of an element in thisLinkedListcannot be stored in the type of the specified array.
-