有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java的LinkedList中的链表peek和元素

peek和Java的LinkedList中的元素有什么区别

以下是OracleJava文档页面对它们的描述,但它们没有解释区别

public E peek()
Retrieves, but does not remove, the head (first element) of this list.
Specified by: peek in interface Deque<E>
Specified by: peek in interface Queue<E>
Returns: the head of this list, or null if this list is empty
Since: 1.5

public E element()
Retrieves, but does not remove, the head (first element) of this list. Specified by: element in interface Deque<E>
Specified by: element in interface Queue<E>
Returns: the head of this list
Throws: NoSuchElementException - if this list is empty
Since: 1.5

如果我们的列表是空的,那么一个抛出异常,另一个返回null,这就是区别吗


共 (1) 个答案