Java源码阅读之ArrayDeque

Summary:

  • public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializabletail
  • 对象中有一个Obejct[]数组;该数组的容量为2的幂
  • tail指向的位置不存储数据;head指向的位置是队列中第一个数据;
  • 数组可以扩容,扩容后大小为原数组长度的2倍
  • 数组中存储的数据有(tail-head)&(elements.length-1)
  • 数据添加的位置是tail;之后tail=(tail+1)&(elements.length-1)
  • 数据移出的位置是head;之后head=(head+1)&(elements.length-1)

Fields:

transient Object[] elements;
transient int head;
transient int tail;
private static final int MIN_INITIAL_CAPACITY = 8;

Constructor:

public ArrayDeque() {
        elements = new Object[16];
}
public ArrayDeque(int numElements) {
        allocateElements(numElements);
}
//将转换的数改成2的幂;这样能将取余运算替换为逻辑与运算
private void allocateElements(int numElements) {
        int initialCapacity = MIN_INITIAL_CAPACITY;
        // Find the best power of two to hold elements.
        // Tests "<=" because arrays aren't kept full.
        if (numElements >= initialCapacity) {
            initialCapacity = numElements;
            initialCapacity |= (initialCapacity >>>  1);
            initialCapacity |= (initialCapacity >>>  2);
            initialCapacity |= (initialCapacity >>>  4);
            initialCapacity |= (initialCapacity >>>  8);
            initialCapacity |= (initialCapacity >>> 16);
            initialCapacity++;

            if (initialCapacity < 0)   // Too many elements, must back off
                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
        }
        elements = new Object[initialCapacity];
}

size()

//注意:这里很巧妙!!
public int size() {
        return (tail - head) & (elements.length - 1);
}

isEmpty() 

public boolean isEmpty() {
        return head == tail;
}

addXX():

添加元素默认将其添加到队列尾端
public boolean add(E e) {
        addLast(e);
        return true;
}
public void addFirst(E e) {
        if (e == null)
            throw new NullPointerException();
        elements[head = (head - 1) & (elements.length - 1)] = e;
        if (head == tail)
            doubleCapacity();
}
public void addLast(E e) {
        if (e == null)
            throw new NullPointerException();
        elements[tail] = e;
        if ( (tail = (tail + 1) & (elements.length - 1)) == head)
            doubleCapacity();
}

removeXX():

移除元素默认移除第一个;队列空抛异常
public E remove() {
        return removeFirst();
}
public E removeFirst() {
        E x = pollFirst();
        if (x == null)
            throw new NoSuchElementException();
        return x;
}


pollXX():

移除第一个元素,队列空则返回null
public E pollFirst() {
        int h = head;
        @SuppressWarnings("unchecked")
        E result = (E) elements[h];
        // Element is null if deque empty
        if (result == null)
            return null;
        elements[h] = null;     // Must null out slot
        head = (h + 1) & (elements.length - 1);
        return result;
}
public E pollLast() {
        int t = (tail - 1) & (elements.length - 1);
        @SuppressWarnings("unchecked")
        E result = (E) elements[t];
        if (result == null)
            return null;
        elements[t] = null;
        tail = t;
        return result;
}


扩容:

//扩容扩两倍
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;
}
































你可能感兴趣的:(java源码,open-jdk,ArrayDeque)