微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 为什么我们可以将空值添加到LinkedList DESPITE强烈劝阻在Deque界面中这样做?

来自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?

最佳答案
允许这样的规定使程序员的生活更轻松.我可以想一个例子.

假设有一个链接列表的房屋和一个postMan来按顺序向每个房子发送信件.
linkedList中的每个节点都存在.房屋有家庭对象作为其价值.
在某些情况下,家庭可能不在那里,那么家庭对象将具有空值.

但对于邮递员来说,这是不相关的,因为他仍然必须访问该节点.
PostOffice建议所有家庭如果他们要离开房子就指示邮局,以便postMan不需要访问那所房子(如果可能的话).

但出于某种目的,房主决定不通知postOffice这种情况,并希望继续收到信件(间接邮寄访问).

原文地址:https://www.jb51.cc/java/437318.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐