为什么ArrayDeque类在pollFirst方法中使用按位运算?

Alb*_*Gao 6 java collections bitwise-operators bitwise-and arraydeque

我通过java源代码看看尝试学习集合的实现.在ArrayDeque类中发现了一件有趣的事情.

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;
}
Run Code Online (Sandbox Code Playgroud)

以下2行是什么意思?这是一个按位操作吗?为什么他们使用它,这里的目的是什么?

    head = (h + 1) & (elements.length - 1);
    int t = (tail - 1) & (elements.length - 1);
Run Code Online (Sandbox Code Playgroud)

我知道一个使用按位的方案是在1个变量中打包2个值.但似乎事实并非如此.

Ori*_*ntz 8

看一下初始化代码 - Deque表示为一个数组,其大小总是2的幂:

195    public ArrayDeque(int numElements) {
196        allocateElements(numElements);
197    }

124    private void allocateElements(int numElements) {
125        int initialCapacity = MIN_INITIAL_CAPACITY;
126        // Find the best power of two to hold elements.
127        // Tests "<=" because arrays aren't kept full.
128        if (numElements >= initialCapacity) {
129            initialCapacity = numElements;
130            initialCapacity |= (initialCapacity >>>  1);
131            initialCapacity |= (initialCapacity >>>  2);
132            initialCapacity |= (initialCapacity >>>  4);
133            initialCapacity |= (initialCapacity >>>  8);
134            initialCapacity |= (initialCapacity >>> 16);
135            initialCapacity++;
136
137            if (initialCapacity < 0)   // Too many elements, must back off
138                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
139        }
140        elements = (E[]) new Object[initialCapacity];
141    }
Run Code Online (Sandbox Code Playgroud)

所以elements.length - 1二进制基本上1是在超出数组大小之前的一系列位.

例如,如果elements初始化为大小为16的数组,elements.length - 1则为15,表示0..001111(截断的前导零).

因此,当head元素在pollFirst方法中重置(由1提前)时,使用按位运算&符以使Deque循环.同样,如果elements大小为16且当前head为15,那么head + 1将为16,因此:

10000
01111
-----
00000
Run Code Online (Sandbox Code Playgroud)

意思head是,重置为索引0.这允许您重用已经分配的空间,使用数组及其插入和检索的O(1)效率,而无需分配新空间.

pollLast重置tail变量的位置也是如此,即如果tail为0且elements大小为16,则:

tail      00000
tail-1    11111   (-1 in two's complement)
          01111
          -----
          01111
Run Code Online (Sandbox Code Playgroud)

含义tail递减一个值,但从0移到elements.length - 1.

您可以使用更"复杂"的if语句(或使用三元运算符)实现相同的功能,但这是实现循环数组的一种相当常见且可接受的方式.