public class

ArrayDeque

extends AbstractCollection<E>
implements Serializable Cloneable Deque<E>
java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.ArrayDeque<E>

Class Overview

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.

Summary

Public Constructors
ArrayDeque()
Constructs 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.
Public Methods
boolean add(E e)
Inserts the element to the tail of the deque.
void addFirst(E e)
Inserts an element at the head of this deque if it dose not violate size limit immediately.
void addLast(E e)
Inserts an element at the tail of this deque if it dose not violate size limit immediately.
void clear()
Empty the deque.
ArrayDeque<E> clone()
Returns a clone of the deque.
boolean contains(Object obj)
Returns true if the specified element is in the deque.
Iterator<E> descendingIterator()
Returns the iterator in reverse order, from tail to head.
E element()
Gets but does not remove the head element of this deque.
E getFirst()
Gets but not removes the head element of this deque.
E getLast()
Gets but not removes the tail element of this deque.
boolean isEmpty()
Returns true if the deque has no elements.
Iterator<E> iterator()
Returns the iterator of the deque.
boolean offer(E e)
Inserts the element at the tail of the deque.
boolean offerFirst(E e)
Inserts an element at the head of this deque unless it would violate size limit.
boolean offerLast(E e)
Inserts an element at the tail of this deque unless it would violate size limit.
E peek()
Gets but not removes the head element of this deque.
E peekFirst()
Gets but not removes the head element of this deque.
E peekLast()
Gets but not removes the tail element of this deque.
E poll()
Gets and removes the head element of this deque.
E pollFirst()
Gets and removes the head element of this deque.
E pollLast()
Gets and removes the tail element of this deque.
E pop()
Pops the head element of the deque, just same as removeFirst().
void push(E e)
Pushes the element to the deque(at the head of the deque), just same as addFirst(E).
E remove()
Gets and removes the head element of this deque.
boolean remove(Object obj)
Removes the first equivalent element of the specified object.
E removeFirst()
Gets and removes the head element of this deque.
boolean removeFirstOccurrence(Object obj)
Removes the first equivalent element of the specified object.
E removeLast()
Gets and removes the tail element of this deque.
boolean removeLastOccurrence(Object obj)
Removes the last equivalent element of the specified object.
int size()
Returns the size of the deque.
<T> T[] toArray(T[] array)
Returns all the elements in an array from head to tail, and the type of the result array is the type of the argument array.
Object[] toArray()
Returns all the elements in an array from head to tail.
[Expand]
Inherited Methods
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.lang.Iterable
From interface java.util.Collection
From interface java.util.Deque
From interface java.util.Queue

Public Constructors

public ArrayDeque ()

Since: API Level 9

Constructs a new empty instance of ArrayDeque big enough for 16 elements.

public ArrayDeque (int minSize)

Since: API Level 9

Constructs a new empty instance of ArrayDeque big enough for specified number of elements.

Parameters
minSize the smallest size of the ArrayDeque

public ArrayDeque (Collection<? extends E> c)

Since: API Level 9

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

Public Methods

public boolean add (E e)

Since: API Level 9

Inserts the element to the tail of the deque.

Parameters
e the element
Returns
  • true

public void addFirst (E e)

Since: API Level 9

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
NullPointerException if the element is null

public void addLast (E e)

Since: API Level 9

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
NullPointerException if the element is null

public void clear ()

Since: API Level 9

Empty the deque.

See Also

public ArrayDeque<E> clone ()

Since: API Level 9

Returns a clone of the deque.

Returns
  • the clone of the deque
See Also

public boolean contains (Object obj)

Since: API Level 9

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

public Iterator<E> descendingIterator ()

Since: API Level 9

Returns the iterator in reverse order, from tail to head.

Returns
  • the reverse order Iterator

public E element ()

Since: API Level 9

Gets but does not remove the head element of this deque. It throws an exception if the deque is empty.

Returns
  • the head element
Throws
NoSuchElementException if the deque is empty
See Also

public E getFirst ()

Since: API Level 9

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
See Also

public E getLast ()

Since: API Level 9

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
See Also

public boolean isEmpty ()

Since: API Level 9

Returns true if the deque has no elements.

Returns
  • true if the deque has no elements, false otherwise
See Also

public Iterator<E> iterator ()

Since: API Level 9

Returns the iterator of the deque. The elements will be ordered from head to tail.

Returns
  • the iterator
See Also

public boolean offer (E e)

Since: API Level 9

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

public boolean offerFirst (E e)

Since: API Level 9

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
Throws
NullPointerException if the element is null

public boolean offerLast (E e)

Since: API Level 9

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
NullPointerException if the element is null

public E peek ()

Since: API Level 9

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

public E peekFirst ()

Since: API Level 9

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

public E peekLast ()

Since: API Level 9

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
See Also

public E poll ()

Since: API Level 9

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

public E pollFirst ()

Since: API Level 9

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

public E pollLast ()

Since: API Level 9

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
See Also

public E pop ()

Since: API Level 9

Pops the head element of the deque, just same as removeFirst().

Returns
  • the head element
Throws
NoSuchElementException if the deque is empty
See Also

public void push (E e)

Since: API Level 9

Pushes the element to the deque(at the head of the deque), just same as addFirst(E).

Parameters
e the element to push
Throws
NullPointerException if the element is null

public E remove ()

Since: API Level 9

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
See Also

public boolean remove (Object obj)

Since: API Level 9

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
  • true if the operation succeeds or false if the deque does not contain the element

public E removeFirst ()

Since: API Level 9

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
See Also

public boolean removeFirstOccurrence (Object obj)

Since: API Level 9

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
  • true if the operation succeeds or false if the deque does not contain the element

public E removeLast ()

Since: API Level 9

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
See Also

public boolean removeLastOccurrence (Object obj)

Since: API Level 9

Removes the last 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
  • true if the operation succeeds or false if the deque does not contain the element.

public int size ()

Since: API Level 9

Returns the size of the deque.

Returns
  • the size of the deque
See Also

public T[] toArray (T[] array)

Since: API Level 9

Returns all the elements in an array from head to tail, and the type of the result array is the type of the argument array. If the argument array is big enough, the elements from the deque will be stored in it(elements following the tail of the deque is set to null, if any); otherwise, it will return a new array with the size of the argument array and size of the deque.

Parameters
array the array stores all the elements from the deque, if it has enough space; otherwise, a new array of the same type and the size of the deque will be used
Returns
  • the Array of all the elements
Throws
ArrayStoreException if the type of the argument array is not compatible with every element in the deque
NullPointerException if the argument array is null
See Also

public Object[] toArray ()

Since: API Level 9

Returns all the elements in an array from head to tail. The result is a copy of all the elements.

Returns
  • the Array of all the elements
See Also