Class ArrayDeque<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Deque<E>, Queue<E>
An implementation of Deque, backed by an array.
ArrayDeques have no size limit, can not contain null element, and they are not thread-safe.
All optional operations are supported, and the elements can be any objects.
Type parameter E: the type of elements in this collection
Since
1.6
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new empty instance of ArrayDeque big enough for 16 elements.ArrayDeque(int minSize) Constructs a new empty instance of ArrayDeque big enough for specified number of elements.ArrayDeque(Collection<? extends E> c) Constructs a new instance of ArrayDeque containing the elements of the specified collection, with the order returned by the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanInserts the element to the tail of the deque.voidInserts an element at the head of this deque if it dose not violate size limit immediately.voidInserts an element at the tail of this deque if it dose not violate size limit immediately.voidclear()Empty the deque.booleanReturns true if the specified element is in the deque.Returns the iterator in reverse order, from tail to head.element()Gets but does not remove the head element of this deque.getFirst()Gets but not removes the head element of this deque.getLast()Gets but not removes the tail element of this deque.booleanisEmpty()Returns true if the deque has no elements.iterator()Returns the iterator of the deque.booleanInserts the element at the tail of the deque.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 not removes the head element of this deque.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 head element of this deque.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 head element of this deque.booleanRemoves the first equivalent element of the specified object.Gets and removes the head element of this deque.booleanRemoves the first equivalent element of the specified object.Gets and removes the tail element of this deque.booleanRemoves the last equivalent element of the specified object.intsize()Returns the size of the deque.Methods inherited from class AbstractCollection
addAll, containsAll, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
-
Constructor Details
-
ArrayDeque
public ArrayDeque()Constructs a new empty instance of ArrayDeque big enough for 16 elements. -
ArrayDeque
public ArrayDeque(int minSize) Constructs a new empty instance of ArrayDeque big enough for specified number of elements.
Parameters
minSize: the smallest size of the ArrayDeque
-
ArrayDeque
Constructs a new instance of ArrayDeque containing the elements of the specified collection, with the order returned by the collection's iterator.
Parameters
c: the source of the elements
Throws
NullPointerException: if the collection is null
-
-
Method Details
-
addFirst
Inserts an element at the head of this deque if it dose not violate size limit immediately. It is better to use offerFirst(E) if a deque is size-limited.
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.
Parameters
e: the element
Throws
NullPointerException: if the element is null
See also
- java.util.Deque#addFirst(java.lang.Object)
-
addLast
Inserts an element at the tail of this deque if it dose not violate size limit immediately. It is better to use offerLast(E) if a deque is size-limited.
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.
Parameters
e: the element
Throws
NullPointerException: if the element is null
See also
- java.util.Deque#addLast(java.lang.Object)
-
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.
Parameters
e: the element
Returns
true
Throws
NullPointerException: if the element is null
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
Parameters
e: the element
Returns
true if the operation succeeds or false if it fails
Throws
NullPointerException: if the element is null
See also
- java.util.Deque#offerLast(java.lang.Object)
-
offer
Inserts the element at the tail of the deque.
Parameters
e: the element
Returns
true if the operation succeeds or false if it fails.
Throws
NullPointerException: if the element is null
See also
- java.util.Queue#offer(java.lang.Object)
-
add
Inserts the element to the tail of the deque.
Parameters
e: the element
Returns
true
See also
- java.util.AbstractCollection#add(java.lang.Object)
- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollection<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.
-
-
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.
Parameters
e: the element to push
Throws
NullPointerException: if the element is null
See also
- java.util.Deque#push(java.lang.Object)
-
removeFirst
Gets and removes the head element of this deque. This method throws an exception if the deque is empty.
Returns
the head element
Throws
NoSuchElementException: if the deque is empty
Returns
the head element
Throws
NoSuchElementException: if the deque is empty
See also
- java.util.Deque#removeFirst()
- Specified by:
removeFirstin interfaceDeque<E>
-
remove
-
pop
Pops the head element of the deque, just same as removeFirst().
Returns
the head element
Throws
NoSuchElementException: if the deque is empty
Returns
the head element
Throws
NoSuchElementException: if the deque is empty
See also
- java.util.Deque#pop()
-
removeLast
Gets and removes the tail element of this deque. This method throws an exception if the deque is empty.
Returns
the tail element
Throws
NoSuchElementException: if the deque is empty
Returns
the tail element
Throws
NoSuchElementException: if the deque is empty
See also
- java.util.Deque#removeLast()
- Specified by:
removeLastin interfaceDeque<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
Returns
the head element or null if the deque is empty
See also
- java.util.Deque#pollFirst()
-
poll
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
- java.util.Queue#poll()
-
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
Returns
the tail element or null if the deque is empty
See also
- java.util.Deque#pollLast()
-
getFirst
Gets but not removes the head element of this deque. This method throws an exception if the deque is empty.
Returns
the head element
Throws
NoSuchElementException: if the deque is empty
Returns
the head element
Throws
NoSuchElementException: if the deque is empty
See also
- java.util.Deque#getFirst()
-
element
-
getLast
Gets but not removes the tail element of this deque. This method throws an exception if the deque is empty.
Returns
the tail element
Throws
NoSuchElementException: if the deque is empty
Returns
the tail element
Throws
NoSuchElementException: if the deque is empty
See also
- java.util.Deque#getLast()
-
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
Returns
the head element or null if the deque is empty
See also
- java.util.Deque#peekFirst()
-
peek
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
- java.util.Queue#peek()
-
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
Returns
the tail element or null if the deque is empty
See also
- java.util.Deque#peekLast()
-
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
Parameters
obj: the element to be removed
Returns
- Specified by:
removeFirstOccurrencein interfaceDeque<E>- Returns:
true if the operation succeeds or false if the deque does not contain the element
See also
- java.util.Deque#removeFirstOccurrence(java.lang.Object)
-
remove
Removes the first equivalent element of the specified object. If the deque does not contain the element, it is unchanged and returns false.
Parameters
obj: the element to be removed
Returns
- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>- Returns:
true if the operation succeeds or false if the deque does not contain the element
See also
- java.util.AbstractCollection#remove(java.lang.Object)
-
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
Parameters
obj: the element to be removed
Returns
- Specified by:
removeLastOccurrencein interfaceDeque<E>- Returns:
true if the operation succeeds or false if the deque does not contain the element.
See also
- java.util.Deque#removeLastOccurrence(java.lang.Object)
-
size
public int size()Returns the size of the deque.
Returns
the size of the deque
See also
- java.util.AbstractCollection#size()
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
isEmpty
public boolean isEmpty()Returns true if the deque has no elements.
Returns
true if the deque has no elements, false otherwise
See also
- java.util.AbstractCollection#isEmpty()
- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.See also
- #size
-
contains
Returns true if the specified element is in the deque.
Parameters
obj: the element
Returns
true if the element is in the deque, false otherwise
See also
- java.util.AbstractCollection#contains(java.lang.Object)
- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueif object is an element of thisCollection,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException if the object to look for isn't of the correct type. -
NullPointerException: @throws NullPointerException if the object to look for isnulland thisCollectiondoesn't supportnullelements.
-
-
clear
public void clear()Empty the deque.
See also
- java.util.AbstractCollection#clear()
- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
iterator
Returns the iterator of the deque. The elements will be ordered from head to tail.
Returns
the iterator
See also
- java.util.AbstractCollection#iterator()
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>
-
descendingIterator
Returns the iterator in reverse order, from tail to head.
Returns
the iterator in reverse order
Returns
the reverse order Iterator
See also
- java.util.Deque#descendingIterator()
- Specified by:
descendingIteratorin interfaceDeque<E>
-