转载请以链接形式标明出处: 本文出自:103style的博客
base on jdk_1.8.0_77
ArrayDeque
简介ArrayDeque
的常量和成员变量介绍ArrayDeque
的构造函数ArrayDeque
相关的函数ArrayDeque
类是双端队列Deque
的实现类,类的继承结构如下:
public class ArrayDeque<E> extends AbstractCollection<E>
implements Deque<E>, Cloneable, Serializable
就其实现而言,ArrayDeque
采用了循环数组的方式来完成双端队列的功能。
ArrayDeque
有以下基本特征:
fast-fail
.Stack
要快.LinkedList
要快。null
元素被禁止使用。private static final int MIN_INITIAL_CAPACITY = 8; //初始化 elements 的 最小长度
transient Object[] elements; // ArrayDeque保存数据的数组
transient int head;// 第一个元素的位置
transient int tail;// 最后一个元素的位置
16
,当指定长度时,会通过allocateElements
计算 大于numElements
的最小 2n
(n >=3)
为数组的长度。 为什么是 2n
,和 HashMap 类似,通过index & (elements.length - 1)
计算索引。public ArrayDeque() {
elements = new Object[16];
}
public ArrayDeque(int numElements) {
allocateElements(numElements);
}
public ArrayDeque(Collection<? extends E> c) {
allocateElements(c.size());
addAll(c);
}
private void allocateElements(int numElements) {
int initialCapacity = MIN_INITIAL_CAPACITY;
if (numElements >= initialCapacity) {
initialCapacity = numElements;
initialCapacity |= (initialCapacity >>> 1);
initialCapacity |= (initialCapacity >>> 2);
initialCapacity |= (initialCapacity >>> 4);
initialCapacity |= (initialCapacity >>> 8);
initialCapacity |= (initialCapacity >>> 16);
initialCapacity++;
if (initialCapacity < 0)
initialCapacity >>>= 1;
}
elements = new Object[initialCapacity];
}
//添加到head前面
public void push(E e)
public boolean offerFirst(E e)
public void addFirst(E e)
//添加到tail后面
public boolean add(E e)
public boolean offerLast(E e)
public void addLast(E e)
//删除head所在位置的元素
public E pop()
public E remove()
public E poll()
public E removeFirst()
public E pollFirst()
//删除 tail所在位置的元素
public E removeLast()
public E pollLast()
//获取head所在位置的元素
public E element()
public E peek()
public E peekFirst()
//获取tail所在位置的元素
public E peekLast()
//获取队列元素的个数
public int size()
head
和 tail
默认为 0
,所以默认第一个元素存入的位置为 element
数组的最后的索引位置。当head==tail
时,说明elements
数组满了,需要进行数组扩容,新数组长度是原来的2
倍,然后重新赋值head = 0
。public void addFirst(E e) {
if (e == null)
throw new NullPointerException();
elements[head = (head - 1) & (elements.length - 1)] = e;
if (head == tail)
doubleCapacity();
}
private void doubleCapacity() {
assert head == tail;
int p = head;
int n = elements.length;
int r = n - p; // number of elements to the right of p
int newCapacity = n << 1;
if (newCapacity < 0)
throw new IllegalStateException("Sorry, deque too big");
Object[] a = new Object[newCapacity];
System.arraycopy(elements, p, a, 0, r);
System.arraycopy(elements, 0, a, r, p);
elements = a;
head = 0;
tail = n;
}
addLast
即赋值 tail
索引位置的值为e
,然后tail++
,当head==tail
时,同addFirst(E e)
进行数组扩容。public void addLast(E e) {
if (e == null)
throw new NullPointerException();
elements[tail] = e;
if ( (tail = (tail + 1) & (elements.length - 1)) == head)
doubleCapacity();
}
public E pollFirst() {
final Object[] elements = this.elements;
final int h = head;
@SuppressWarnings("unchecked")
E result = (E) elements[h];
// Element is null if deque empty
if (result != null) {
elements[h] = null; // Must null out slot
head = (h + 1) & (elements.length - 1);
}
return result;
}
public E pollLast() {
final Object[] elements = this.elements;
final int t = (tail - 1) & (elements.length - 1);
@SuppressWarnings("unchecked")
E result = (E) elements[t];
if (result != null) {
elements[t] = null;
tail = t;
}
return result;
}
public E peekFirst() {
// elements[head] is null if deque empty
return (E) elements[head];
}
public E peekLast() {
return (E) elements[(tail - 1) & (elements.length - 1)];
}
public int size() {
return (tail - head) & (elements.length - 1);
}
ArrayDeque
是带首尾标记
的数组实现的双端队列。以上