public class ArrayDeque<E>

  1. Object
  2. AbstractCollection<E>
  3. ArrayDeque

ImplementsCollection<E>, Deque<E>, Iterable<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

Constructors

public ArrayDeque()Constructs a new empty instance of ArrayDeque big enough for 16 elements.
public ArrayDeque(int minSize)Constructs a new empty instance of ArrayDeque big enough for specified number of elements.
public 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.

Methods

public void addFirst(E e)Inserts an element at the head of this deque if it dose not violate size limit immediately.
public void addLast(E e)Inserts an element at the tail of this deque if it dose not violate size limit immediately.
public boolean offerFirst(E e)Inserts an element at the head of this deque unless it would violate size limit.
public boolean offerLast(E e)Inserts an element at the tail of this deque unless it would violate size limit.
public boolean offer(E e)Inserts the element at the tail of the deque.
public boolean add(E e)Inserts the element to the tail of the deque.
public void push(E e)Pushes the element to the deque(at the head of the deque), just same as addFirst(E).
public E removeFirst()Gets and removes the head element of this deque.
public E remove()Gets and removes the head element of this deque.
public E pop()Pops the head element of the deque, just same as removeFirst().
public E removeLast()Gets and removes the tail element of this deque.
public E pollFirst()Gets and removes the head element of this deque.
public E poll()Gets and removes the head element of this deque.
public E pollLast()Gets and removes the tail element of this deque.
public E getFirst()Gets but not removes the head element of this deque.
public E element()Gets but does not remove the head element of this deque.
public E getLast()Gets but not removes the tail element of this deque.
public E peekFirst()Gets but not removes the head element of this deque.
public E peek()Gets but not removes the head element of this deque.
public E peekLast()Gets but not removes the tail element of this deque.
public boolean removeFirstOccurrence(Object obj)Removes the first equivalent element of the specified object.
public boolean remove(Object obj)Removes the first equivalent element of the specified object.
public boolean removeLastOccurrence(Object obj)Removes the last equivalent element of the specified object.
public int size()Returns the size of the deque.
public boolean isEmpty()Returns true if the deque has no elements.
public boolean contains(Object obj)Returns true if the specified element is in the deque.
public void clear()Empty the deque.
public Iterator<E> iterator()Returns the iterator of the deque.
public Iterator<E> descendingIterator()Returns the iterator in reverse order, from tail to head.

Inherited methods

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 int
the smallest size of the ArrayDeque

ArrayDeque

public 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.

Parameters

c Collection<? extends E>
the source of the elements

Throws

NullPointerException
if the collection is null

Method details

addFirst

public void addFirst(E e)
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 E
the element

Throws

NullPointerException
if the element is null
IllegalStateException
if it can not add now due to size limit
ClassCastException
if the class of element can not be added into this deque
IllegalArgumentException
if the element can not be added due to some property.

addLast

public void addLast(E e)
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 E
the element

Throws

NullPointerException
if the element is null
IllegalStateException
if it can not add now due to size limit
ClassCastException
if the class of element can not be added into this deque
IllegalArgumentException
if the element can not be added due to some property.

offerFirst

public boolean offerFirst(E e)
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 E
the element

Returns

true

Throws

NullPointerException
if the element is null
ClassCastException
if the class of element can not be added into this deque
IllegalArgumentException
if the element can not be added due to some property.

offerLast

public boolean offerLast(E e)
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 E
the element

Returns

true if the operation succeeds or false if it fails

Throws

NullPointerException
if the element is null
ClassCastException
if the class of element can not be added into this deque
IllegalArgumentException
if the element can not be added due to some property

offer

public boolean offer(E e)
Inserts the element at the tail of the deque.

Parameters

e E
the element

Returns

true if the operation succeeds or false if it fails.

Throws

NullPointerException
if the element is null

add

public boolean add(E e)
Inserts the element to the tail of the deque.

Parameters

e E
the element

Returns

true

push

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

Parameters

e E
the element to push

Throws

NullPointerException
if the element is null
IllegalStateException
if it can not add now due to size limit
ClassCastException
if the class of element can not be added into this deque
IllegalArgumentException
if the element can not be added due to some property.

removeFirst

public E 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

remove

public E remove()
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

pop

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

Returns

the head element

Throws

NoSuchElementException
if the deque is empty

removeLast

public E 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

pollFirst

public 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

poll

public E 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

pollLast

public E 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

getFirst

public E 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

element

public E element()
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

getLast

public E 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

peekFirst

public E 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

peek

public E 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

peekLast

public E 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

removeFirstOccurrence

public boolean removeFirstOccurrence(Object obj)
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 Object
the element to be removed

Returns

true if the operation succeeds or false if the deque does not contain the element

Throws

ClassCastException
if the class of the element is incompatible with the deque
NullPointerException
if the element is null and the deque can not contain null element

remove

public boolean remove(Object obj)
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 Object
the element to be removed

Returns

true if the operation succeeds or false if the deque does not contain the element

removeLastOccurrence

public boolean removeLastOccurrence(Object obj)
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 Object
the element to be removed

Returns

true if the operation succeeds or false if the deque does not contain the element.

Throws

ClassCastException
if the class of the element is incompatible with the deque
NullPointerException
if the element is null and the deque can not contain null element

size

public int size()
Returns the size of the deque.

Returns

the size of the deque

isEmpty

public boolean isEmpty()
Returns true if the deque has no elements.

Returns

true if the deque has no elements, false otherwise

contains

public boolean contains(Object obj)
Returns true if the specified element is in the deque.

Parameters

obj Object
the element

Returns

true if the element is in the deque, false otherwise

clear

public void clear()
Empty the deque.

Throws

UnsupportedOperationException
it the iterator does not support removing elements from this Collection

iterator

public Iterator<E> iterator()
Returns the iterator of the deque. The elements will be ordered from head to tail.

Returns

the iterator

descendingIterator

public Iterator<E> descendingIterator()
Returns the iterator in reverse order, from tail to head.

Returns

the reverse order Iterator