[英]LinkedList Get method
我有一個單鏈表的 get 方法,它們工作正常,但我的導師告訴我他希望我減少代碼,因為我有太多的特殊情況。 問題是,當我嘗試刪除代碼的某些部分時,代碼不再按預期工作。
我寫的代碼:
public E get(int index) {
Node<E> f = first;
// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index > size || index < 0){
throw new IndexOutOfBoundsException();
}
// Index Less than size and is not the first or last node.
if (index < size && index > 0) {
for (int i = 0; i < index; i++) {
f = f.next;
}
return f.getValue();
}
// If the Linked List is empty + Index = 0 return null
if (first == null && index == 0) {
return null;
}
// If Linked List is not empty and index = 0 return first value
if (index == 0) {
return first.getValue();
}
// If index = end of list
if (index == size) {
return last.getValue();
}
// Return null if not found.
return null;
}
所以他告訴我,我想太多了,只需要兩種情況,如果索引有效或無效; 我同意他的看法,因此我嘗試將代碼縮短為:
Node<E> f = first;
// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index > size || index < 0){
throw new IndexOutOfBoundsException();
}
// Index Less than size and is not the first or last node.
if (index <= size && index >= 0) {
for (int i = 0; i < index; i++) {
f = f.next;
}
}
return f.getValue();
對於我的測試用例,我使用具有以下值的鏈表:
[弗蘭克、喬治、喬什、吉姆、瑪麗、蘇西、約翰、吉姆、達科他、利維、傑克遜、傑夫、沃爾特、馬特]
我的測試用例在我的測試類中如下:
System.out.println("Get Method Test ----------------------------");
System.out.println("First Index: " + ll.get(0));
System.out.println("Last Index: " + ll.get(ll.size()));
System.out.println("'Middle' Index: " + ll.get(5));
System.out.println("Empty list with index of 0: " + llempty.get(0));
在嘗試獲取最后一個索引的第二個測試用例中拋出NullPointerException
:
輸出:
First Index: Frank
Exception in thread "main" java.lang.NullPointerException
我需要能夠證明以下幾點:
測試用例:索引為零,列表末尾的索引,列表“中間”的索引,索引為0的空列表,索引太大,索引太小
所以我被困在這里伙計們/女孩們,任何幫助將不勝感激!
我只會讓你的代碼更簡單(第二個if
不需要你的循環):
//assume 0 based index
int current = 0;
//loop until we hit the requested index
while (current != index) {
f = f.next;
current++;
}
//return the right node
return f;
假設您對索引越界的初始檢查是正確的,這永遠不會給您帶來問題。 但是,如果您的索引是基於 0 的,則需要將越界檢查更改為:
if (index > size - 1 || index < 0) {
例如,如果有 2 個元素,則索引為 0 和 1。在這種情況下,索引 2 無效。
您的代碼中有兩個相同錯誤的實例。 您必須了解索引是零相關的,這意味着大小為n
的列表中的最后一個元素將具有索引n-1
。 第一個錯誤是在你的新get()
方法中:
// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index > size || index < 0){
throw new IndexOutOfBoundsException();
}
在這里您應該檢查index >= size
,如下所示:
// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index >= size || index < 0){
throw new IndexOutOfBoundsException();
}
第二個錯誤是在您的測試代碼中。 你寫的地方
System.out.println("Last Index: " + ll.get(ll.size()));
您應該使用ll.size() - 1
如下:
System.out.println("Last Index: " + ll.get(ll.size() - 1));
這兩件事都被其他人觀察到了。 但是,有必要同時修復它們。
當我進行這些更改並運行您的測試時,它到達最后一行並按預期拋出IndexOutOfBoundsException
。 我還測試了“索引太小”和“索引太大”並得到了IndexOutOfBoundsException
。
這可能不是唯一的原因(您只向我們展示了部分代碼),但您弄亂了“大小”值,因為如果索引從 0 開始(像往常一樣),最后一個元素應該在
index == size -1
本來應該
if (index >= size || index < 0){
throw new IndexOutOfBoundsException();
}
if (index < size && index >= 0) {
for (int i = 0; i < index; i++) {
f = f.next;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.