java - 为什么我们可以向 LinkedList 添加 null 值,尽管在 Deque 接口(interface)中强烈反对这样做?

标签 java data-structures

来自 Deque 的 Javadoc:

While Deque implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of any Deque implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so because null is used as a special return value by various methods to indicated that the deque is empty.

我没有在现有问题中找到答案:Why can we add null elements to a java LinkedList?

最佳答案

允许这样的规定使程序员的生活更轻松。我能想到一个例子。

假设有一个 linkedList 的房子,一个 postman 来按顺序向每个房子投递信件。 linkedList 中的每个节点都存在。房子有一个家庭对象作为他们的值(value)。 在某些情况下,家庭可能不存在,那么 house 对象将具有空值。

但对于 postman 来说,这无关紧要,因为他仍然必须访问该节点。 邮局建议所有家庭在离开家时注明邮局,这样 postman 就不需要去那所房子(如果可能的话)。

但出于某种原因,房主决定不将这种情况通知邮局,并希望继续接收信件( postman 间接访问)。

关于java - 为什么我们可以向 LinkedList 添加 null 值,尽管在 Deque 接口(interface)中强烈反对这样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112348/

相关文章:

javascript - 在 JavaScript 中构建一棵树

java - 如何确定数据结构的实际内存使用情况

python - 树的数据结构

java - 如何获取包含日期、小时和分钟的 ISO 8601 格式的当前时刻?

java - 试图在公共(public)静态空间中繁殖。任何帮助表示赞赏

java - JSESSIONID 出现在 URL 中后,我的 Web 应用程序停止工作

java调用其他类中的方法无法识别更改的变量

java - 取消我没有写的动态加载的 Callable

c++ - 为什么我可以在 C++ 中的函数中定义结构和类?

c# - C++ vector 与C#列表。他们为什么产生不同的结果?