Class ArrayDeque<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.ArrayDeque<E>
- Type Parameters:
E- the type of elements in this collection
- 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.
- 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.- Specified by:
addFirstin interfaceDeque<E>- Parameters:
e- the element- Throws:
NullPointerException- if the element is null- See Also:
-
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.- Specified by:
addLastin interfaceDeque<E>- Parameters:
e- the element- Throws:
NullPointerException- if the element is null- 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
- Throws:
NullPointerException- if the element is null- 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.- Specified by:
offerLastin interfaceDeque<E>- Parameters:
e- the element- Returns:
- true if the operation succeeds or false if it fails
- Throws:
NullPointerException- if the element is null- See Also:
-
offer
Inserts the element at the tail of the deque.- Specified by:
offerin interfaceQueue<E>- Parameters:
e- the element- Returns:
- true if the operation succeeds or false if it fails.
- Throws:
NullPointerException- if the element is null- See Also:
-
add
Inserts the element to the tail of the deque.- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
e- the element- Returns:
- true
- See Also:
-
push
Pushes the element to the deque(at the head of the deque), just same as addFirst(E).- Specified by:
pushin interfaceDeque<E>- Parameters:
e- the element to push- Throws:
NullPointerException- if the element is null- See Also:
-
removeFirst
Gets and removes the head element of this deque. This method throws an exception if the deque is empty.- Specified by:
removeFirstin interfaceDeque<E>- Returns:
- the head element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
remove
Gets and removes the head element of this deque. This method throws an exception if the deque is empty.- Specified by:
removein interfaceQueue<E>- Returns:
- the head element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
pop
Pops the head element of the deque, just same as removeFirst().- Specified by:
popin interfaceDeque<E>- Returns:
- the head element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
removeLast
Gets and removes the tail element of this deque. This method throws an exception if the deque is empty.- Specified by:
removeLastin interfaceDeque<E>- Returns:
- the tail element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
pollFirst
-
poll
-
pollLast
-
getFirst
Gets but not removes the head element of this deque. This method throws an exception if the deque is empty.- Specified by:
getFirstin interfaceDeque<E>- Returns:
- the head element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
element
Gets but does not remove the head element of this deque. It throws an exception if the deque is empty.- Specified by:
elementin interfaceQueue<E>- Returns:
- the head element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
getLast
Gets but not removes the tail element of this deque. This method throws an exception if the deque is empty.- Specified by:
getLastin interfaceDeque<E>- Returns:
- the tail element
- Throws:
NoSuchElementException- if the deque is empty- See Also:
-
peekFirst
-
peek
-
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.- Specified by:
removeFirstOccurrencein interfaceDeque<E>- Parameters:
obj- the element to be removed- Returns:
- true if the operation succeeds or false if the deque does not contain the element
- See Also:
-
remove
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:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
obj- the element to be removed- Returns:
- true if the operation succeeds or false if the deque does not contain the element
- 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:
obj- the element to be removed- Returns:
- true if the operation succeeds or false if the deque does not contain the element.
- See Also:
-
size
public int size()Returns the size of the deque.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the size of the deque
- See Also:
-
isEmpty
public boolean isEmpty()Returns true if the deque has no elements.- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
- true if the deque has no elements, false otherwise
- See Also:
-
contains
Returns true if the specified element is in the deque.- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
obj- the element- Returns:
- true if the element is in the deque, false otherwise
- See Also:
-
clear
public void clear()Empty the deque.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>- See Also:
-
iterator
-
descendingIterator
Returns the iterator in reverse order, from tail to head.- Specified by:
descendingIteratorin interfaceDeque<E>- Returns:
- the reverse order Iterator
- See Also:
-