首页 \ 问答 \ 为什么ArrayDeque类在pollFirst方法中使用按位运算?(Why the ArrayDeque class use bitwise operation in the pollFirst method?)

为什么ArrayDeque类在pollFirst方法中使用按位运算?(Why the ArrayDeque class use bitwise operation in the pollFirst method?)

我通过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;
}

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

    head = (h + 1) & (elements.length - 1);
    int t = (tail - 1) & (elements.length - 1);

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


I look through java source code try to learn the implementation of collection. Found a interesting thing in the ArrayDeque class.

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;
}

What does the following 2 lines mean? Is it a bitwise operation? Why they use it and what's the purpose here?

    head = (h + 1) & (elements.length - 1);
    int t = (tail - 1) & (elements.length - 1);

I know one scenario to use the bitwise is to pack 2 values in 1 variable. But it seems this is not the case.


原文:https://stackoverflow.com/questions/36825213
更新时间:2022-01-04 17:01

最满意答案

该网站在Chrome浏览器中使用F12发送了大量错误消息,显然你的www-embed-player-new.js导致了这个问题。 甚至Edge都没有正确运行网站。 我认为您需要Chrome扩展程序才能使其正常运行。

我建议你更改导入视频的插件。 点击此处输入链接说明

编辑:Chrome插件谷歌搜索停止错误消息,但仍然无法正常工作。


The problem was with the wordpress loop on the single.php page. A wrong code snippet caused the issue.

Fixed!

相关问答

更多