2017-02-13 44 views
-3

我是編程的初學者,我需要編寫某種自己的LinkedList,但只能用add(E element)方法和Iterator的hasNext()next()。這裏是我的代碼:NPE自己實現的LinkedList(Java)

public class LinkedArray<E> implements Iterator<E> { 

     private int size = 0; 

     private int current = 0; 

     private Node<E> first; 

     private Node<E> last; 

     private Objects[] objects = new Objects[10]; 

     public void add(E value) { 
      Node<E> element = new Node<E>(last, value, null); 
      if (last != null) { 
       element.next = element; 
      } else { 
       first = element; 
      } 
      last = element; 
      size++; 
     } 

     @Override 
     public boolean hasNext() { 
      boolean result = false; 
      try { 
       if (objects[current + 1] != null) { 
        result = true; 
       } 
      } catch (ArrayIndexOutOfBoundsException e) { 
       result = false; 
      } 
      return result; 
     } 

     @Override 
     public E next() { 
      E result; 
      try { 
       current++; 
       result = (get(current - 1)); 
      } catch (ArrayIndexOutOfBoundsException a) { 
       throw new NoSuchElementException("No more elements in list."); 
      } 
      return result; 
     } 

    public E get(int position) throws NullPointerException { 
     Object result; 
    if (this.objects[position] != null) { 
     result = this.objects[position]; 
    } else { 
     throw new NullPointerException("Position is empty."); 
    } 
    return (E) result; 
} 


     private class Node<E> { 

      private E element; 

      private Node<E> next; 

      private Node<E> prev; 

      Node(Node<E> prev, E element, Node<E> next) { 
       this.element = element; 
       this.next = next; 
       this.prev = prev; 
      } 
     } 
    } 

但是當我開始測試添加(E值)...

@Test 
    public void test() { 
     LinkedArray<String> arr = new LinkedArray<>(); 
     String string = "Test"; 

     arr.add(string); 
     String result = arr.next(); 

     assertThat(result, is("Test")); 
    } 

...我只能得到一個錯誤。問題是什麼,爲什麼我錯了?

+0

發佈錯誤(stacktrace)你正在得到 – hanumant

+0

@hanumant它只是「java.lang.NullPointerException:位置爲空。」在E get(int position)方法中。 – blackHorsie

+2

然後要麼它沒有被正確添加,或者你沒有正確訪問它 –

回答

2

你明確地拋出了你自己的NPE。

public E get(int position) throws NullPointerException { 
    Object result; 
    if (this.objects[position] != null) { 
     result = this.objects[position]; 
    } else { 
     throw new NullPointerException("Position is empty."); 
    } 
    return (E) result; 
} 

如果你想跟隨the get() method contract of a List,的Javadoc說這

拋出:
IndexOutOfBoundsException - 如果該指數超出範圍(index < 0 || index >= size())

所以你既然是不是「引用」任何是null,而只是去return null在你的數組爲空的情況下,然後拋出其他異常。

public E get(int position) throws IndexOutOfBoundsException { 
    if (position < 0 || position >= this.objects.length) { 
     throw new IndexOutOfBoundsException(); 
    } 
    return (E) this.objects[position]; 
} 

注:Iterator類通常不會有E get()方法。只是hasNext()next()

所以,你不應該實現你的類的方式,next()要求get()通話。您也不需要try-catch那裏。你已經知道何時position超出使用if語句的範圍。

0

您的get(...)函數讀取objects數組中的對象。 你從來沒有設置這個數組的內容,所以如果position小於10那麼它將永遠是null並導致你遇到的NPE。

看起來你已經改編了一個基於數組的列表,但只改變了add方法。所有其他方法與空的objects數組進行交互。