2017-02-13 3 views
-3

Ich bin ein Anfänger in Programmierung und ich muss eine Art eigene LinkedList schreiben, aber nur mit add(E element) Methode und mit Iterator hasNext() und next(). Hier ist mein Code:NPE in eigener Realisierung von 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; 
      } 
     } 
    } 

Aber als ich anfing, in den Test (E-Wert) ...

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

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

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

... ich nur einen Fehler. Was ist das Problem, warum bekomme ich es falsch?

+0

Post den Fehler (Stacktrace) Sie erhalten – hanumant

+0

@hanumant es ist nur "java.lang.NullPointerException: Position ist leer." in E get (int Position) -Methode. – blackHorsie

+2

dann entweder wird es nicht korrekt hinzugefügt oder Sie greifen nicht richtig darauf zu –

Antwort

2

Sie haben explizit Ihre eigene NPE geworfen.

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; 
} 

Wenn Sie the get() method contract of a List folgen wollen, sagt der Javadoc diese

Würfe:
IndexOutOfBoundsException - wenn der Index, da Sie sich außerhalb der Reichweite (index < 0 || index >= size())

Daher ist es nicht "referenzieren" etwas, das ist null, und eher nur gehen zu return null Wenn Ihr Array leer ist, werfen Sie die andere Ausnahme.

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

Hinweis: Iterator Klassen in der Regel nicht über eine E get() Methode. Nur hasNext() und next()

Daher sollten Sie nicht Ihre Klasse in einer Weise umzusetzen, dass next() einen Aufruf an get() erfordert.

Sie brauchen auch keine try-catch dort. Sie wissen bereits, wenn position mit if-Anweisungen außerhalb der Grenzen liegt.

0

Ihre get(...) Funktion liest die Objekte aus dem Array objects. Sie setzen den Inhalt dieses Arrays niemals so, wenn position kleiner als 10 ist, wird es immer null sein und die NPE verursachen, die Sie gefunden haben.

Offenbar haben Sie eine Array-basierte Liste angepasst, aber nur die add-Methode geändert. Alle anderen Methoden interagieren mit dem leeren Array objects.