Java源码阅读之ArrayDeque
2016-03-14 15:14
447 查看
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; }
相关文章推荐
- arduino 控制led灯亮度强弱变化的经典代码
- easyUI datagrid editor 修改 type
- String、StringBuilder、StringBuffer的区别
- String、StringBuffer与StringBuilder之间区别
- UITextField加密输入属性
- for应用,转义字符,break和continue的区别
- invalidate()和requestLayout()区别
- query的结构某字段如何在WEIBI上单独定义格式
- iOS 基础知识之UI篇
- 关于UITableView
- 修改UITextfield的Placeholder字体的颜色
- UITableViewCell 自定义绘制 性能高
- freeradius build
- SQL--使用NewID函数,创建GUID列
- 【error】git clone: SSL certificate problem: unable to get local issuer certificate
- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- Angular-Ui-Router
- poj 2533 Longest Ordered Subsequence(dp)
- 根据Request获取客户端IP
- Polymorphism (C# Programming Guide)