2017-02-13 94 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数组进行交互。