<学习记录>Queue源码学习

来源:互联网 发布:java路径分隔符 编辑:程序博客网 时间:2024/06/03 12:10

  • Queue实现方式(ArrayDeque和LinkedList)
  • ArrayDeque:循环数组elements保存元素

transient Object[] elements;transient int head;transient int tail;

  • 构造
public ArrayDeque() {        elements = new Object[16];}public ArrayDeque(int numElements) {        allocateElements(numElements);}

调整数组长度:
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];    }
解析:根据特定参数设置初始数组的大小。中间移位和或操作是将initialCapacity左边最高位的1复制到右边,再加1变成2的幂次方。
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;    }
解析:这个数组长度扩展,当元素个数增长到等于数组大小时(head==tail),此时将数组大小扩充一倍,再使用arraycopy将元素复制到新的数组。arraycopy(src,srcPos,dest,destPos,length),src指原数组,srcPos指数组复制的开始位置,dest指目的数组,destPos指目的数组的起始位置,length指要复制的长度。
  • 添加
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();    }
public boolean offerFirst(E e) {        addFirst(e);        return true;    }
public boolean offerLast(E e) {        addLast(e);        return true;    }
添加是将新的元素添加到队头或者队尾,add和offer的功能实现是一样的。因为已经初始化了数组大小,出头元素时head往后移动,如尾元素时tail往后移动,当移动到数组最后时,会将head或者tail循环到数组的前面。所以
head = (head - 1) & (elements.length - 1)
的功能相当于
head = (head - 1) %(element.length - 1)
由于tail指向的是队尾的后一位,所以先入队尾,再
(tail = (tail + 1) & (elements.length - 1)
功能相当于
tail = (tail + 1) % (elements.length - 1)
当tail==head时,说明元素已满,所以要扩充数组大小,并使用arraycopy拷贝数组。
  • 移除
public E removeFirst() {        E x = pollFirst();        if (x == null)            throw new NoSuchElementException();        return x;    }
public E removeLast() {        E x = pollLast();        if (x == null)            throw new NoSuchElementException();        return x;    }
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;    }
解析:poll和remove都是移除元素操作,实现方法一样。出队头元素,将头元素为null,将head移动到下一个位置;
head = (h + 1) & (elements.length - 1);
出队尾元素,将最后一个元素为null,tail指向前一个位置。
t = (tail - 1) & (elements.length - 1);
  • 获取元素
public E getFirst() {        @SuppressWarnings("unchecked")        E result = (E) elements[head];        if (result == null)            throw new NoSuchElementException();        return result;    }
public E getLast() {        @SuppressWarnings("unchecked")        E result = (E) elements[(tail - 1) & (elements.length - 1)];        if (result == null)            throw new NoSuchElementException();        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)];    }
解析:peek和get的方法实现一样。主要是获取头元素和尾元素的数组下标。



0 0
原创粉丝点击