public class ArrayDeque
extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable
| java.lang.Object | ||
| java.util.AbstractCollection<E> | ||
| java.util.ArrayDeque<E> | ||
Deque接口的可调整大小的实现。 Array deques没有容量限制; 它们根据需要增长以支持使用。 它们不是线程安全的; 在没有外部同步的情况下,它们不支持多线程的并发访问。 禁止使用空元素。 当用作堆栈时,该类可能会比Stack更快,并且在用作队列时速度会比LinkedList更快。
大多数ArrayDeque操作以摊销后的恒定时间运行。 例外包括remove , removeFirstOccurrence , removeLastOccurrence , contains , iterator.remove() ,和批量操作,所有这些都在运行线性时间。
这个类的iterator方法返回的迭代器是快速失败的 :如果在创建迭代器后随时修改双端队列,除了通过迭代器自己的方法remove ,迭代器通常会抛出ConcurrentModificationException 。 因此,面对并发修改,迭代器快速而干净地失败,而不是在将来某个未确定的时间冒着任意的,非确定性的行为风险。
请注意,迭代器的故障快速行为无法得到保证,因为一般来说,在存在非同步并发修改的情况下不可能做出任何硬性保证。 失败快速迭代器在尽力而为的基础上抛出ConcurrentModificationException 。 因此,编写一个依赖于此异常的程序是正确的: 迭代器的快速失败行为应仅用于检测错误。
该类及其迭代器实现 Collection和 Iterator接口的所有 可选方法。
Public constructors |
|
|---|---|
ArrayDeque() 使用足以容纳16个元素的初始容量构造一个空数组deque。 |
|
ArrayDeque(int numElements) 构造一个空数组deque,其初始容量足以容纳指定数量的元素。 |
|
ArrayDeque(Collection<? extends E> c) 按照集合迭代器返回的顺序构造一个包含指定集合元素的deque。 |
|
公共方法(Public methods) |
|
|---|---|
boolean |
add(E e) 在此双端队列的末尾插入指定的元素。 |
void |
addFirst(E e) 在此双端队列的前面插入指定的元素。 |
void |
addLast(E e) 在此双端队列的末尾插入指定的元素。 |
void |
clear() 删除此双端队列中的所有元素。 |
ArrayDeque<E> |
clone() 返回此双端队列的副本。 |
boolean |
contains(Object o) 如果此双端队列包含指定的元素,则返回 |
Iterator<E> |
descendingIterator() 以相反顺序返回此双端队列中元素的迭代器。 |
E |
element() 检索但不移除由此双端队列表示的队列头部。 |
E |
getFirst() 检索但不删除此双端队列的第一个元素。 |
E |
getLast() 检索但不删除此双端队列的最后一个元素。 |
boolean |
isEmpty() 如果此双端队列不包含元素,则返回 |
Iterator<E> |
iterator() 返回此双端队列中元素的迭代器。 |
boolean |
offer(E e) 在此双端队列的末尾插入指定的元素。 |
boolean |
offerFirst(E e) 在此双端队列的前面插入指定的元素。 |
boolean |
offerLast(E e) 在此双端队列的末尾插入指定的元素。 |
E |
peek() 检索但不移除由此双端队列表示的队列头,或者如果此双端队列为空,则返回 |
E |
peekFirst() 检索但不移除此双端队列的第一个元素,或者如果此双端队列为空,则返回 |
E |
peekLast() 检索但不移除此双端队列的最后一个元素,或者如果此双端队列为空,则返回 |
E |
poll() 检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素);如果此双端队列为空,则返回 |
E |
pollFirst() 检索并移除此双端队列的第一个元素,或者如果此双端队列为空,则返回 |
E |
pollLast() 检索并移除此双端队列的最后一个元素,或者如果此双端队列为空,则返回 |
E |
pop() 从由此双端队列表示的堆栈中弹出一个元素。 |
void |
push(E e) 将元素推入由此双端队列表示的堆栈。 |
E |
remove() 检索并删除由此双端队列表示的队列的头部。 |
boolean |
remove(Object o) 从此双端队列中移除指定元素的单个实例。 |
E |
removeFirst() 检索并删除此双端队列的第一个元素。 |
boolean |
removeFirstOccurrence(Object o) 删除此双端队列中首次出现的指定元素(当从头到尾遍历双端队列时)。 |
E |
removeLast() 检索并删除此双端队列的最后一个元素。 |
boolean |
removeLastOccurrence(Object o) 删除此双端队列中指定元素的最后一次出现(当从头到尾遍历双端队列时)。 |
int |
size() 返回此双端队列中的元素数量。 |
Spliterator<E> |
spliterator() 在此双端队列中的元素上创建 late-binding和 快速失败 |
Object[] |
toArray() 以适当的顺序返回一个包含此双端队列中所有元素的数组(从第一个元素到最后一个元素)。 |
<T> T[] |
toArray(T[] a) 以适当的顺序返回一个包含此双端队列中所有元素的数组(从第一个元素到最后一个元素); 返回数组的运行时类型是指定数组的运行时类型。 |
继承方法(Inherited methods) |
|
|---|---|
java.util.AbstractCollection
|
|
java.lang.Object
|
|
java.util.Collection
|
|
java.util.Deque
|
|
java.lang.Iterable
|
|
java.util.Queue
|
|
ArrayDeque (int numElements)
构造一个空数组deque,其初始容量足以容纳指定数量的元素。
| 参数(Parameters) | |
|---|---|
numElements |
int: lower bound on initial capacity of the deque |
ArrayDeque (Collection<? extends E> c)
按照集合迭代器返回的顺序构造一个包含指定集合元素的deque。 (集合迭代器返回的第一个元素成为第一个元素,或者deque的前面 。)
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection whose elements are to be placed into the deque |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified collection is null |
boolean add (E e)
在此双端队列的末尾插入指定的元素。
该方法相当于 addLast(E) 。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true (as specified by add(E)) |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
void addFirst (E e)
在此双端队列的前面插入指定的元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
void addLast (E e)
在此双端队列的末尾插入指定的元素。
这种方法相当于 add(E) 。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
ArrayDeque<E> clone ()
返回此双端队列的副本。
| 返回(Returns) | |
|---|---|
ArrayDeque<E> |
a copy of this deque |
boolean contains (Object o)
如果此双端队列包含指定的元素,则返回true 。 更正式地,返回true当且仅当该双端包含至少一个元素e ,使得o.equals(e) 。
| 参数(Parameters) | |
|---|---|
o |
Object: object to be checked for containment in this deque |
| 返回(Returns) | |
|---|---|
boolean |
true if this deque contains the specified element |
Iterator<E> descendingIterator ()
以相反顺序返回此双端队列中元素的迭代器。 元素将从上一个(尾)到第一个(头)的顺序返回。
| 返回(Returns) | |
|---|---|
Iterator<E> |
an iterator over the elements in this deque in reverse sequence |
E element ()
检索但不移除由此双端队列表示的队列头部。 此方法与peek仅在于,如果此双端队列为空,则会引发异常。
该方法相当于 getFirst() 。
| 返回(Returns) | |
|---|---|
E |
the head of the queue represented by this deque |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
E getFirst ()
检索但不删除此双端队列的第一个元素。 该方法与peekFirst仅在于,如果此双端队列为空,则会引发异常。
| 返回(Returns) | |
|---|---|
E |
the head of this deque |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
E getLast ()
检索但不删除此双端队列的最后一个元素。 此方法与peekLast仅在于,如果此双端队列为空,则会引发异常。
| 返回(Returns) | |
|---|---|
E |
the tail of this deque |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
boolean isEmpty ()
如果此双端队列不包含元素,则返回 true 。
| 返回(Returns) | |
|---|---|
boolean |
true if this deque contains no elements |
Iterator<E> iterator ()
返回此双端队列中元素的迭代器。 元素将从第一个(头部)到最后一个(尾部)排序。 这与元素出队的顺序相同(通过连续呼叫remove()或弹出(通过连续呼叫pop() )。
| 返回(Returns) | |
|---|---|
Iterator<E> |
an iterator over the elements in this deque |
boolean offer (E e)
在此双端队列的末尾插入指定的元素。
该方法相当于 offerLast(E) 。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true (as specified by offer(E)) |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
boolean offerFirst (E e)
在此双端队列的前面插入指定的元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true (as specified by offerFirst(E)) |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
boolean offerLast (E e)
在此双端队列的末尾插入指定的元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true (as specified by offerLast(E)) |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
E peek ()
检索但不移除由此双端队列表示的队列头,或者如果此双端队列为空,则返回 null 。
该方法相当于 peekFirst() 。
| 返回(Returns) | |
|---|---|
E |
the head of the queue represented by this deque, or null if this deque is empty |
E peekFirst ()
检索但不移除此双端队列的第一个元素,或者如果此双端队列为空,则返回 null 。
| 返回(Returns) | |
|---|---|
E |
the head of this deque, or null if this deque is empty |
E peekLast ()
检索但不删除此双端队列的最后一个元素,或者如果此双端队列为空,则返回 null 。
| 返回(Returns) | |
|---|---|
E |
the tail of this deque, or null if this deque is empty |
E poll ()
检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素);如果此双端队列为空,则返回 null 。
该方法相当于 pollFirst() 。
| 返回(Returns) | |
|---|---|
E |
the head of the queue represented by this deque, or null if this deque is empty |
E pollFirst ()
检索并删除此双端队列的第一个元素,或者如果此双端队列为空,则返回 null 。
| 返回(Returns) | |
|---|---|
E |
the head of this deque, or null if this deque is empty |
E pollLast ()
检索并移除此双端队列的最后一个元素,或者如果此双端队列为空,则返回 null 。
| 返回(Returns) | |
|---|---|
E |
the tail of this deque, or null if this deque is empty |
E pop ()
从由此双端队列表示的堆栈中弹出一个元素。 换句话说,删除并返回此双端队列的第一个元素。
该方法相当于 removeFirst() 。
| 返回(Returns) | |
|---|---|
E |
the element at the front of this deque (which is the top of the stack represented by this deque) |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
void push (E e)
将元素推入由此双端队列表示的堆栈。 换句话说,将元素插入此双端队列的前端。
该方法相当于 addFirst(E) 。
| 参数(Parameters) | |
|---|---|
e |
E: the element to push |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified element is null |
E remove ()
检索并删除由此双端队列表示的队列的头部。 此方法与poll仅在于,如果此双端队列为空,则会引发异常。
该方法相当于 removeFirst() 。
| 返回(Returns) | |
|---|---|
E |
the head of the queue represented by this deque |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
boolean remove (Object o)
从此双端队列中移除指定元素的单个实例。 如果该deque不包含该元素,则该值不变。 更正式地说,删除第一个元素e ,使得o.equals(e) (如果存在这样的元素)。 如果此双端队列包含指定的元素(或等效地,如果此双端队列因呼叫而改变),则返回true 。
这种方法相当于 removeFirstOccurrence(Object) 。
| 参数(Parameters) | |
|---|---|
o |
Object: element to be removed from this deque, if present |
| 返回(Returns) | |
|---|---|
boolean |
true if this deque contained the specified element |
E removeFirst ()
检索并删除此双端队列的第一个元素。 此方法与pollFirst仅在于,如果此双端队列为空,则会引发异常。
| 返回(Returns) | |
|---|---|
E |
the head of this deque |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
boolean removeFirstOccurrence (Object o)
删除此双端队列中首次出现的指定元素(当从头到尾遍历双端队列时)。 如果该deque不包含该元素,则该值不变。 更正式地说,删除第一个元素e ,使得o.equals(e) (如果存在这样的元素)。 如果此双端队列包含指定的元素(或等效地,如果此双端队列由于调用而更改),则返回true 。
| 参数(Parameters) | |
|---|---|
o |
Object: element to be removed from this deque, if present |
| 返回(Returns) | |
|---|---|
boolean |
true if the deque contained the specified element |
E removeLast ()
检索并删除此双端队列的最后一个元素。 此方法与pollLast仅在于,如果此双端队列为空,则会引发异常。
| 返回(Returns) | |
|---|---|
E |
the tail of this deque |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
|
boolean removeLastOccurrence (Object o)
删除此双端队列中指定元素的最后一次出现(当从头到尾遍历双端队列时)。 如果该deque不包含该元素,则该值不变。 更正式地说,删除最后一个元素e ,使得o.equals(e) (如果存在这样的元素)。 如果此双端队列包含指定的元素(或等效地,如果此双端队列由于调用而更改),则返回true 。
| 参数(Parameters) | |
|---|---|
o |
Object: element to be removed from this deque, if present |
| 返回(Returns) | |
|---|---|
boolean |
true if the deque contained the specified element |
int size ()
返回此双端队列中的元素数量。
| 返回(Returns) | |
|---|---|
int |
the number of elements in this deque |
Spliterator<E> spliterator ()
在此双端队列中的元素上创建一个 late-binding和 快速失败 Spliterator 。
该Spliterator报告SIZED , SUBSIZED , ORDERED ,并NONNULL 。 重写实现应记录附加特征值的报告。
| 返回(Returns) | |
|---|---|
Spliterator<E> |
a Spliterator over the elements in this deque |
Object[] toArray ()
以适当的顺序返回一个包含此双端队列中所有元素的数组(从第一个元素到最后一个元素)。
返回的数组将是“安全的”,因为此双端队列没有引用它。 (换句话说,这个方法必须分配一个新的数组)。 调用者可以自由修改返回的数组。
此方法充当基于数组和基于集合的API之间的桥梁。
| 返回(Returns) | |
|---|---|
Object[] |
an array containing all of the elements in this deque |
T[] toArray (T[] a)
以适当的顺序返回一个包含此双端队列中所有元素的数组(从第一个元素到最后一个元素); 返回数组的运行时类型是指定数组的运行时类型。 如果deque适合指定的数组,则返回其中。 否则,将使用指定数组的运行时类型和此双端队列的大小分配一个新数组。
如果此双端队列适合指定阵列,并有空余空间(即阵列中的元素多于此双端队列),则紧跟在双端队列末尾的阵列中的元素将设置为 null 。
与toArray()方法一样,此方法充当基于数组和基于集合的API之间的桥梁。 此外,该方法允许精确控制输出数组的运行时类型,并且在某些情况下可以用于节省分配成本。
假设x是一个已知只包含字符串的deque。 下面的代码可以用来将deque转储到新分配的数组String :
String[] y = x.toArray(new String[0]); Note that
toArray(new Object[0]) is identical in function to
toArray().
| 参数(Parameters) | |
|---|---|
a |
T: 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 |
| 返回(Returns) | |
|---|---|
T[] |
an array containing all of the elements in this deque |
| 抛出异常(Throws) | |
|---|---|
ArrayStoreException |
if the runtime type of the specified array is not a supertype of the runtime type of every element in this deque |
NullPointerException |
if the specified array is null |