gpt4 book ai didi

java - Java Queue 接口(interface)中的方法之间有什么区别?

转载 作者:行者123 更新时间:2023-11-29 04:49:56 25 4
gpt4 key购买 nike

检查 Java 中的队列接口(interface) here

我对方法定义感到困惑:

element() Retrieves, but does not remove, the head of this queue.

就像

peek() Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

如果队列为空,我可以看到 peek 返回 null 但实际上两者都定义为

Throws: NoSuchElementException - if this queue is empty

同样适用于 remove()peek()

这背后有什么目标还是只是一个设计陷阱?

最佳答案

让我们用一个应该回答您的问题的表格来澄清事情:

+---------+---------------------------+------------------------+
| method | action | throws when not found? |
+---------+---------------------------+------------------------+
| peek | Retrieves, doesn't remove | NO |
| remove | Retrieves, removes | YES |
| element | Retrieves, doesn't remove | YES |
+---------+---------------------------+------------------------+

有些方法在没有找到元素时返回null,其他方法则抛出异常。

有些方法只检索但不删除,有些方法检索并删除。

关于java - Java Queue 接口(interface)中的方法之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com