Class: java.util.ArrayDeque<E>
- public class ArrayDeque<E>
- extends AbstractCollection<E>
- implements Deque<E>, Cloneable, Serializable
Most ArrayDeque operations run in amortized constant time. Exceptions include remove, removeFirstOccurrence, removeLastOccurrence, contains, iterator.remove(), and the bulk operations, all of which run in linear time.
The iterators returned by this class's iterator method are fail-fast: If the deque is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will generally throw a java.util.ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
This class and its iterator implement all of the optional methods of the java.util.Collection and java.util.ArrayDeque$DeqIterator interfaces.
This class is a member of the Java Collections Framework.
Inheritance
Superclass tree:- java.lang.Object
- java.util.AbstractCollection<E>
- java.util.ArrayDeque
- Deque<E>
- Cloneable
- Serializable
- Queue<E>
- Collection<E>
- Iterable<E>
Methods
-
ArrayDequetop
public ArrayDeque()Constructs an empty array deque with an initial capacity sufficient to hold 16 elements. -
ArrayDequetop
public ArrayDeque(int numElements)Constructs an empty array deque with an initial capacity sufficient to hold the specified number of elements. -
ArrayDequetop
public ArrayDeque(Collection<? extends E> c)Constructs a deque containing the elements of the specified collection, in the order they are returned by the collection's iterator. (The first element returned by the collection's iterator becomes the first element, or front of the deque.) -
addtop
public boolean add(E e)Inserts the specified element at the end of this deque.This method is equivalent to java.util.ArrayDeque.addLast(java.lang.Object).
- Override hierarchy:
- add from AbstractCollection<E>
-
addFirsttop
public void addFirst(E e)Inserts the specified element at the front of this deque. -
addLasttop
public void addLast(E e)Inserts the specified element at the end of this deque.This method is equivalent to java.util.ArrayDeque.add(java.lang.Object).
-
allocateElementstop
private void allocateElements(int numElements)Allocate empty array to hold the given number of elements. -
checkInvariantstop
private void checkInvariants() -
cleartop
public void clear()Removes all of the elements from this deque. The deque will be empty after this call returns.- Specified by:
- clear from Collection<E>
- Override hierarchy:
- clear from AbstractCollection<E>
-
clonetop
public ArrayDeque<E> clone()Returns a copy of this deque. -
containstop
public boolean contains(Object o)Returns true if this deque contains the specified element. More formally, returns true if and only if this deque contains at least one element e such that o.equals(e).- Specified by:
- contains from Deque<E>
- contains from Collection<E>
- Override hierarchy:
- contains from AbstractCollection<E>
-
copyElementstop
private <T> T[] copyElements(T[] a)Copies the elements from our element array into the specified array, in order (from first to last element in the deque). It is assumed that the array is large enough to hold all elements in the deque. -
deletetop
private boolean delete(int i)Removes the element at the specified position in the elements array, adjusting head and tail as necessary. This can result in motion of elements backwards or forwards in the array.This method is called delete rather than remove to emphasize that its semantics differ from those of java.util.List.remove(int).
-
descendingIteratortop
public Iterator<E> descendingIterator()Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).- Specified by:
- descendingIterator from Deque<E>
-
doubleCapacitytop
private void doubleCapacity()Double the capacity of this deque. Call only when full, i.e., when head and tail have wrapped around to become equal. -
elementtop
public E element()Retrieves, but does not remove, the head of the queue represented by this deque. This method differs from peek only in that it throws an exception if this deque is empty.This method is equivalent to java.util.ArrayDeque.getFirst().
-
getFirsttop
public E getFirst()Retrieves, but does not remove, the first element of this deque. This method differs from peekFirst only in that it throws an exception if this deque is empty. -
getLasttop
public E getLast()Retrieves, but does not remove, the last element of this deque. This method differs from peekLast only in that it throws an exception if this deque is empty. -
isEmptytop
public boolean isEmpty()Returns true if this deque contains no elements.- Specified by:
- isEmpty from Collection<E>
- Override hierarchy:
- isEmpty from AbstractCollection<E>
-
iteratortop
public Iterator<E> iterator()Returns an iterator over the elements in this deque. The elements will be ordered from first (head) to last (tail). This is the same order that elements would be dequeued (via successive calls to java.util.ArrayDeque.remove() or popped (via successive calls to java.util.ArrayDeque.pop()).- Override hierarchy:
- iterator from AbstractCollection<E>
-
offertop
public boolean offer(E e)Inserts the specified element at the end of this deque.This method is equivalent to java.util.ArrayDeque.offerLast(java.lang.Object).
-
offerFirsttop
public boolean offerFirst(E e)Inserts the specified element at the front of this deque.- Specified by:
- offerFirst from Deque<E>
-
offerLasttop
public boolean offerLast(E e)Inserts the specified element at the end of this deque. -
peektop
public E peek()Retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.This method is equivalent to java.util.ArrayDeque.peekFirst().
-
peekFirsttop
public E peekFirst()Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. -
peekLasttop
public E peekLast()Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. -
polltop
public E poll()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.This method is equivalent to java.util.ArrayDeque.pollFirst().
-
pollFirsttop
public E pollFirst()Retrieves and removes the first element of this deque, or returns null if this deque is empty. -
pollLasttop
public E pollLast()Retrieves and removes the last element of this deque, or returns null if this deque is empty. -
poptop
public E pop()Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.This method is equivalent to java.util.ArrayDeque.removeFirst().
-
pushtop
public void push(E e)Pushes an element onto the stack represented by this deque. In other words, inserts the element at the front of this deque.This method is equivalent to java.util.ArrayDeque.addFirst(java.lang.Object).
-
readObjecttop
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundExceptionDeserialize this deque. -
removetop
public E remove()Retrieves and removes the head of the queue represented by this deque. This method differs from poll only in that it throws an exception if this deque is empty.This method is equivalent to java.util.ArrayDeque.removeFirst().
-
removetop
public boolean remove(Object o)Removes a single instance of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that o.equals(e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).This method is equivalent to java.util.ArrayDeque.removeFirstOccurrence(java.lang.Object).
- Specified by:
- remove from Deque<E>
- remove from Collection<E>
- Override hierarchy:
- remove from AbstractCollection<E>
-
removeFirsttop
public E removeFirst()Retrieves and removes the first element of this deque. This method differs from pollFirst only in that it throws an exception if this deque is empty.- Specified by:
- removeFirst from Deque<E>
-
removeFirstOccurrencetop
public boolean removeFirstOccurrence(Object o)Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail). If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that o.equals(e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- Specified by:
- removeFirstOccurrence from Deque<E>
-
removeLasttop
public E removeLast()Retrieves and removes the last element of this deque. This method differs from pollLast only in that it throws an exception if this deque is empty.- Specified by:
- removeLast from Deque<E>
-
removeLastOccurrencetop
public boolean removeLastOccurrence(Object o)Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail). If the deque does not contain the element, it is unchanged. More formally, removes the last element e such that o.equals(e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- Specified by:
- removeLastOccurrence from Deque<E>
-
sizetop
public int size()Returns the number of elements in this deque.- Specified by:
- size from Deque<E>
- size from Collection<E>
- Override hierarchy:
- size from AbstractCollection<E>
-
toArraytop
public Object[] toArray()Returns an array containing all of the elements in this deque in proper sequence (from first to last element).The returned array will be "safe" in that no references to it are maintained by this deque. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
- Specified by:
- toArray from Collection<E>
- Override hierarchy:
- toArray from AbstractCollection<E>
-
toArraytop
public <T> T[] toArray(T[] a)Returns an array containing all of the elements in this deque in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the deque fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this deque.If this deque fits in the specified array with room to spare (i.e., the array has more elements than this deque), the element in the array immediately following the end of the deque is set to null.
Like the java.util.ArrayDeque.toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Suppose x is a deque known to contain only strings. The following code can be used to dump the deque into a newly allocated array of String:
String[] y = x.toArray(new String[0]);Note that toArray(new Object[0]) is identical in function to toArray().- Specified by:
- toArray from Collection<E>
- Override hierarchy:
- toArray from AbstractCollection<E>
-
writeObjecttop
private void writeObject(ObjectOutputStream s) throws IOExceptionSerialize this deque.
Fields
-
MIN_INITIAL_CAPACITY
static final private int MIN_INITIAL_CAPACITY = 8The minimum capacity that we'll use for a newly created deque. Must be a power of 2. -
elements
transient private E[] elementsThe array in which the elements of the deque are stored. The capacity of the deque is the length of this array, which is always a power of two. The array is never allowed to become full, except transiently within an addX method where it is resized (see doubleCapacity) immediately upon becoming full, thus avoiding head and tail wrapping around to equal each other. We also guarantee that all array cells not holding deque elements are always null. -
head
transient private int headThe index of the element at the head of the deque (which is the element that would be removed by remove() or pop()); or an arbitrary number equal to tail if the deque is empty. -
serialVersionUID
static final private long serialVersionUID = 2340985798034038923Appease the serialization gods. -
tail
transient private int tailThe index at which the next element would be added to the tail of the deque (via addLast(E), add(E), or push(E)).
