In Java's LinkedList implementation, I see two methods which seems to me like having identical functions.
在Java的LinkedList实现中,我看到了两种方法,在我看来,它们具有相同的功能。
getFirst()
--Returns the first element in this list.
getFirst()——返回列表中的第一个元素。
peekFirst()
--Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
peekFirst()——检索但不删除列表的第一个元素,如果列表为空,则返回null。
Both of them get the pointer to the First element in the LinkedList without making any changes to it. Then, what's the difference ?
它们都获取指向LinkedList中第一个元素的指针,而不做任何修改。那么,有什么区别呢?
The only difference I see is that peekFirst
returns null
if the list is empty and getFirst
throws a NoSuchElementException
if the list is empty. What was the use of such a design pattern ?
我看到的唯一不同是,如果列表为空,peekFirst返回null,如果列表为空,则getFirst抛出NoSuchElementException。这种设计模式的用途是什么?
3 个解决方案
#1
6
Java introduced LinkedList
in version 1.2. This is when the getFirst
method has been provided. This message threw NoSuchElementException
when the list is empty, causing programmers to do an extra check before the call:
Java在1.2版中引入了LinkedList。这时提供了getFirst方法。该消息在列表为空时抛出NoSuchElementException,导致程序员在调用之前进行额外检查:
Element e = null;
if (!myList.isEmpty()) {
e = myList.getFirst();
}
This was an inconvenience, which has been fixed in Java version 1.6 by adding the peekFirst
method and other methods of the Dequeue<T>
interface.
这是一个麻烦,在Java版本1.6中通过添加Dequeue
#2
1
Only one reson: 1) It reduces the Exception Handling while development
只有一个reson: 1)它减少了开发时的异常处理
public E peekFirst() {
if (size==0)
return null;
return getFirst();
}
Above is the implementation of peekFirst(), it just check the size ZERO, and returns NULL instead of throwing Exception
上面是peekFirst()的实现,它只检查大小为0,返回NULL而不是抛出异常
#3
1
LinkedList is a Deque. Deque API defines methods which exist in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).
链表是一个双端队列。Deque API定义了以两种形式存在的方法:一种方法在操作失败时抛出异常,另一种方法返回一个特殊值(根据操作的不同,要么为null,要么为false)。
#1
6
Java introduced LinkedList
in version 1.2. This is when the getFirst
method has been provided. This message threw NoSuchElementException
when the list is empty, causing programmers to do an extra check before the call:
Java在1.2版中引入了LinkedList。这时提供了getFirst方法。该消息在列表为空时抛出NoSuchElementException,导致程序员在调用之前进行额外检查:
Element e = null;
if (!myList.isEmpty()) {
e = myList.getFirst();
}
This was an inconvenience, which has been fixed in Java version 1.6 by adding the peekFirst
method and other methods of the Dequeue<T>
interface.
这是一个麻烦,在Java版本1.6中通过添加Dequeue
#2
1
Only one reson: 1) It reduces the Exception Handling while development
只有一个reson: 1)它减少了开发时的异常处理
public E peekFirst() {
if (size==0)
return null;
return getFirst();
}
Above is the implementation of peekFirst(), it just check the size ZERO, and returns NULL instead of throwing Exception
上面是peekFirst()的实现,它只检查大小为0,返回NULL而不是抛出异常
#3
1
LinkedList is a Deque. Deque API defines methods which exist in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).
链表是一个双端队列。Deque API定义了以两种形式存在的方法:一种方法在操作失败时抛出异常,另一种方法返回一个特殊值(根据操作的不同,要么为null,要么为false)。