除了 "null is used as a special return value" 之外,ArrayDeque 中是否还有其他原因不允许使用 NULL?

Is there any other reason why NULL is not allowed in ArrayDeque besides "null is used as a special return value"?

在您尝试查找重复问题或否决票之前,请让我解释一下:)

我想我阅读了关于此主题的大部分 Whosebug 答案。他们都总结为:

Null should not be inserted into a Queue, as null is also used as a special return value by some methods to indicate that the queue contains no elements.

他们完全正确(让我们忽略这里的LinkedList)。例如,如果我要调用 queue.poll()queue.peek() 并且 return 类型是 null,我不知道 null 表示null为元素null为无元素.

但是任何有基础知识的程序员都可以这样做来避免给定的问题:

            //queue initialization..            
            queue.add(null);
            if (!queue.isEmpty()){
                System.out.println(queue.poll());
            }

所以我必须再问一次。 “null 是特殊的 return 类型”是它在大多数 Queue 实现中是 'banned' 的唯一原因(LinkedList 除外)吗?或者我遗漏了一些关于这种情况的额外信息?

队列实现本可以设计为允许空条目,但事实并非如此。这是一个设计权衡:'flexibility' 能够插入空条目,而繁琐的编程接口必须先检查队列是否包含任何元素,然后才能调用一个方法,该方法部分地确定是否队列包含任何元素。

线索就在名字里'poll'。如果 poll 不能执行 'remove and return; else indicate nothing to return',那么 poll 不是一个有用的方法;空的测试,然后是现有的 'remove' 调用,就可以完成工作。

因此,我想队列设计者考虑的是一个包含 'things' 而不是什么都没有的队列(即,他们按字面意思理解这个词)并接受了该限制以方便single-method 投票电话。

简而言之,你的答案(IMO)是'yes, that is the reason',但我认为理由很好。