Class: java.util.ArrayDeque<E>

Resizable-array implementation of the java.util.Deque interface. Array deques have no capacity restrictions; they grow as necessary to support usage. They are not thread-safe; in the absence of external synchronization, they do not support concurrent access by multiple threads. Null elements are prohibited. This class is likely to be faster than java.util.Stack when used as a stack, and faster than java.util.LinkedList when used as a queue.

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.

Authors:
@author Josh Bloch and Doug Lea
Parameters:
@param <E> the type of elements held in this collection
Since:
@since 1.6

Inheritance

Superclass tree: Implements:

Methods

  • ArrayDequetop

    public ArrayDeque()
    Constructs an empty array deque with an initial capacity sufficient to hold 16 elements.
    Google Code Search
    Stack Overflow
  • ArrayDequetop

    public ArrayDeque(int numElements)
    Constructs an empty array deque with an initial capacity sufficient to hold the specified number of elements.
    Parameters:
    @param numElements lower bound on initial capacity of the deque
    Google Code Search
    Stack Overflow
  • 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.)
    Parameters:
    @param c the collection whose elements are to be placed into the deque
    Exceptions:
    @throws NullPointerException if the specified collection is null
    Google Code Search
    Stack Overflow
  • 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).

    Parameters:
    @param e the element to add
    Return:
    @return true (as specified by java.util.Collection.add(java.lang.Object) )
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    add from Deque<E>
    add from Queue<E>
    add from Collection<E>
    Override hierarchy:
    add from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • addFirsttop

    public void addFirst(E e)
    Inserts the specified element at the front of this deque.
    Parameters:
    @param e the element to add
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    addFirst from Deque<E>
    Google Code Search
    Stack Overflow
  • 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).

    Parameters:
    @param e the element to add
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    addLast from Deque<E>
    Google Code Search
    Stack Overflow
  • allocateElementstop

    private void allocateElements(int numElements)
    Allocate empty array to hold the given number of elements.
    Parameters:
    @param numElements the number of elements to hold
    Google Code Search
    Stack Overflow
  • checkInvariantstop

    private void checkInvariants()
    Google Code Search
    Stack Overflow
  • 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>
    Google Code Search
    Stack Overflow
  • clonetop

    public ArrayDeque<E> clone()
    Returns a copy of this deque.
    Return:
    @return a copy of this deque
    Override hierarchy:
    clone from Object
    Google Code Search
    Stack Overflow
  • 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).
    Parameters:
    @param o object to be checked for containment in this deque
    Return:
    @return true if this deque contains the specified element
    Specified by:
    contains from Deque<E>
    contains from Collection<E>
    Override hierarchy:
    contains from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • 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.
    Return:
    @return its argument
    Google Code Search
    Stack Overflow
  • 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).

    Return:
    @return true if elements moved backwards
    Google Code Search
    Stack Overflow
  • 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).
    Return:
    @return an iterator over the elements in this deque in reverse sequence
    Specified by:
    descendingIterator from Deque<E>
    Google Code Search
    Stack Overflow
  • 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.
    Google Code Search
    Stack Overflow
  • 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().

    Return:
    @return the head of the queue represented by this deque
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    element from Deque<E>
    element from Queue<E>
    Google Code Search
    Stack Overflow
  • 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.
    Return:
    @return the head of this deque
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    getFirst from Deque<E>
    Google Code Search
    Stack Overflow
  • 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.
    Return:
    @return the tail of this deque
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    getLast from Deque<E>
    Google Code Search
    Stack Overflow
  • isEmptytop

    public boolean isEmpty()
    Returns true if this deque contains no elements.
    Return:
    @return true if this deque contains no elements
    Specified by:
    isEmpty from Collection<E>
    Override hierarchy:
    isEmpty from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • 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()).
    Return:
    @return an iterator over the elements in this deque
    Specified by:
    iterator from Deque<E>
    iterator from Collection<E>
    iterator from Iterable<E>
    Override hierarchy:
    iterator from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • 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).

    Parameters:
    @param e the element to add
    Return:
    @return true (as specified by java.util.Queue.offer(java.lang.Object) )
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    offer from Deque<E>
    offer from Queue<E>
    Google Code Search
    Stack Overflow
  • offerFirsttop

    public boolean offerFirst(E e)
    Inserts the specified element at the front of this deque.
    Parameters:
    @param e the element to add
    Return:
    @return true (as specified by java.util.Deque.offerFirst(java.lang.Object) )
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    offerFirst from Deque<E>
    Google Code Search
    Stack Overflow
  • offerLasttop

    public boolean offerLast(E e)
    Inserts the specified element at the end of this deque.
    Parameters:
    @param e the element to add
    Return:
    @return true (as specified by java.util.Deque.offerLast(java.lang.Object) )
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    offerLast from Deque<E>
    Google Code Search
    Stack Overflow
  • 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().

    Return:
    @return the head of the queue represented by this deque, or null if this deque is empty
    Specified by:
    peek from Deque<E>
    peek from Queue<E>
    Google Code Search
    Stack Overflow
  • peekFirsttop

    public E peekFirst()
    Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
    Return:
    @return the head of this deque, or null if this deque is empty
    Specified by:
    peekFirst from Deque<E>
    Google Code Search
    Stack Overflow
  • peekLasttop

    public E peekLast()
    Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
    Return:
    @return the tail of this deque, or null if this deque is empty
    Specified by:
    peekLast from Deque<E>
    Google Code Search
    Stack Overflow
  • 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().

    Return:
    @return the head of the queue represented by this deque, or null if this deque is empty
    Specified by:
    poll from Deque<E>
    poll from Queue<E>
    Google Code Search
    Stack Overflow
  • pollFirsttop

    public E pollFirst()
    Retrieves and removes the first element of this deque, or returns null if this deque is empty.
    Return:
    @return the head of this deque, or null if this deque is empty
    Specified by:
    pollFirst from Deque<E>
    Google Code Search
    Stack Overflow
  • pollLasttop

    public E pollLast()
    Retrieves and removes the last element of this deque, or returns null if this deque is empty.
    Return:
    @return the tail of this deque, or null if this deque is empty
    Specified by:
    pollLast from Deque<E>
    Google Code Search
    Stack Overflow
  • 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().

    Return:
    @return the element at the front of this deque (which is the top of the stack represented by this deque)
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    pop from Deque<E>
    Google Code Search
    Stack Overflow
  • 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).

    Parameters:
    @param e the element to push
    Exceptions:
    @throws NullPointerException if the specified element is null
    Specified by:
    push from Deque<E>
    Google Code Search
    Stack Overflow
  • readObjecttop

    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
    Deserialize this deque.
    Google Code Search
    Stack Overflow
  • 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().

    Return:
    @return the head of the queue represented by this deque
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    remove from Deque<E>
    remove from Queue<E>
    Google Code Search
    Stack Overflow
  • 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).

    Parameters:
    @param o element to be removed from this deque, if present
    Return:
    @return true if this deque contained the specified element
    Specified by:
    remove from Deque<E>
    remove from Collection<E>
    Override hierarchy:
    remove from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • 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.
    Return:
    @return the head of this deque
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    removeFirst from Deque<E>
    Google Code Search
    Stack Overflow
  • 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).
    Parameters:
    @param o element to be removed from this deque, if present
    Return:
    @return true if the deque contained the specified element
    Specified by:
    removeFirstOccurrence from Deque<E>
    Google Code Search
    Stack Overflow
  • 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.
    Return:
    @return the tail of this deque
    Exceptions:
    @throws NoSuchElementException if this deque is empty
    Specified by:
    removeLast from Deque<E>
    Google Code Search
    Stack Overflow
  • 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).
    Parameters:
    @param o element to be removed from this deque, if present
    Return:
    @return true if the deque contained the specified element
    Specified by:
    removeLastOccurrence from Deque<E>
    Google Code Search
    Stack Overflow
  • sizetop

    public int size()
    Returns the number of elements in this deque.
    Return:
    @return the number of elements in this deque
    Specified by:
    size from Deque<E>
    size from Collection<E>
    Override hierarchy:
    size from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • 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.

    Return:
    @return an array containing all of the elements in this deque
    Specified by:
    toArray from Collection<E>
    Override hierarchy:
    toArray from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • 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().
    Parameters:
    @param a the array into which the elements of the deque are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose
    Return:
    @return an array containing all of the elements in this deque
    Exceptions:
    @throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this deque
    @throws NullPointerException if the specified array is null
    Specified by:
    toArray from Collection<E>
    Override hierarchy:
    toArray from AbstractCollection<E>
    Google Code Search
    Stack Overflow
  • writeObjecttop

    private void writeObject(ObjectOutputStream s) throws IOException
    Serialize this deque.
    Misc:
    @serialData The current size (int) of the deque, followed by all of its elements (each an object reference) in first-to-last order.
    Google Code Search
    Stack Overflow

Fields

  • MIN_INITIAL_CAPACITY

    static final private int MIN_INITIAL_CAPACITY = 8
    The minimum capacity that we'll use for a newly created deque. Must be a power of 2.
  • elements

    transient private E[] elements
    The 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 head
    The 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 = 2340985798034038923
    Appease the serialization gods.
  • tail

    transient private int tail
    The index at which the next element would be added to the tail of the deque (via addLast(E), add(E), or push(E)).