2

DoublyLinkListの割り当てを完了するための割り当てがあり、2つの方法でいくつかの問題があります。私はMyListとMyAbstractListのコードを与えられ、私のDoublyLinkedListでMyAbstractListを拡張するように言われました。 MyListまたはMyAbstractListを変更することはできません。ここで DoublyLinkedList後方への移動の問題

は私が提供されたマイリストためのコードです:

public interface MyList<E extends Comparable<E>> { 

     /** Return true if this list contains no elements */ 
     public boolean isEmpty(); 

     /** Return the number of elements in this list */ 
     public int size(); 

     /** Add a new element at the proper point */ 
     public void add(E e); 

     /** Clear the list */ 
     public void clear(); 

     /** Return true if this list contains the element */ 
     public boolean contains(E e); 

     /** Return the element from this list at the specified index */ 
     public E get(int index); 

     /** Return the first element in the list */ 
     public E getFirst(); 

     /** Return the last element in the list */ 
     public E getLast(); 

     /** Return the index of the first matching element in this list. 
     * Return -1 if no match. */ 
     public int indexOf(E e); 

     /** Return the index of the last matching element in this list 
     * Return -1 if no match. */ 
     public int lastIndexOf(E e); 

     /** Remove the first occurrence of the element o from this list. 
     * Shift any subsequent elements to the left. 
     * Return true if the element is removed. */ 
     public boolean remove(E e); 

     /** Remove the element at the specified position in this list 
     * Shift any subsequent elements to the left. 
     * Return the element that was removed from the list. */ 
     public boolean remove(int index); 

     /** Remove the first element in the list, return true if done, false if list is empty */ 
     public boolean removeFirst(); 

     /** Remove the Last element in the list, return true if done, false if list is empty */ 
     public boolean removeLast(); 

     /** Replace the element at the specified position in this list 
     * with the specified element and return true if done, false if index out of range. */ 
     public boolean set(int index, E e); 

    } 

、これは私がMyAbstractListのために提供されたコードだった。

public abstract class MyAbstractList<E extends Comparable<E>> implements MyList<E> { 
    protected int size = 0; // The size of the list 

    /** Create a default list */ 
    protected MyAbstractList() { 
    } 

    /** Create a list from an array of objects */ 
    public MyAbstractList(E[] objects) { 
     for (int i = 0; i < objects.length; i++) 
      add(objects[i]); 
    } 

@Override /** Return true if this list contains no elements */ 
    public boolean isEmpty() { 
    return size == 0; 
    } 

    @Override /** Return the number of elements in this list */ 
    public int size() { 
    return size; 
    } 

} 

最後に、ここではMyDoublyLinkedListための私のコードです

import java.util.ArrayList; 
public class MyDoublyLinkedList<E extends Comparable<E>> extends MyAbstractList<E> 
{ 
    int size =0; 
    Node<E> head = null; 
    Node<E> current = null; 
    Node<E> tail = null; 
    public MyDoublyLinkedList() { 

    } 

    /** Create a list from an array of objects */ 
    public MyDoublyLinkedList(E[] objects) { 
     super(objects); 
    } 

    @Override 
     public boolean isEmpty() 
     { 
     return size == 0; 
     } 

    @Override 
     public int size() 
     { 
      return size; 
     } 
    /** Add a new element at the proper point */ 
    @Override 
     public void add(E e) 
     { 
     Node<E> newNode = new Node<E>(e); 
     if (tail == null) { 
      head = tail = newNode; 

     } 
     else //if (head != null) 
     { 
      tail.next = newNode; 
      tail = newNode; 

     }  

     size++; 

    } 

    @Override 
     public void clear() 
     { 
      size = 0; 
      head = tail = null; 
     } 

    @Override 
     public boolean contains(E e) 
     { 
     Node<E> current = head; 
     for (int i = 0; i < size; i++) { 
     if (current.element.equals(e)) 
      return true; 
     current = current.next; 
     } 

     return false; 
     } 

    @Override 
     public E get(int index) 
     { 
     if (index < 0 || index > size - 1) 
     return null; 

     Node<E> current = head; 
     for (int i = 0; i < index; i++) 
     current = current.next; 

     return current.element; 
     } 

    @Override 
     public E getFirst() 
     { 
     if (size == 0) { 
      return null; 
     } 
     else { 
      return head.element; 
     } 
     } 

    @Override 
     public E getLast() 
     { 
     if (size == 0) { 
      return null; 
     } 
     else { 
      return tail.element; 
     } 
     } 

    @Override 
     public int indexOf(E e) 
     { 
      Node<E> current = head; 
     for (int i = 0; i < size; i++) { 
     if (current.element.equals(e)) 
      return i; 
     current = current.next; 
     } 

     return -1; 
     } 

    @Override 
     public int lastIndexOf(E e) 
     { 
     int lastIndex = -1; 
     Node<E> current = head; 
     for (int i = 0; i < size; i++) { 
     if (current.element.equals(e)) 
      lastIndex = i; 
     current = current.next; 
     } 

     return lastIndex; 
     } 
    /** Remove the first occurrence of the element o from this list. 
    * Shift any subsequent elements to the left. 
    * Return true if the element is removed. */ 
    @Override 
     public boolean remove(E e) 
     { 
     int index = indexOf(e); 
     if (index != -1) { 
      return remove(index); 
     } else { 
      return false; 
     } 



     } 
    /** Remove the element at the specified position in this list 
    * Shift any subsequent elements to the left. 
    * Return the element that was removed from the list. */ 
    @Override 
     public boolean remove(int index) 
     { 
     if (index < 0 || index >= size) { 
      return false; 
     } 
     else if (index == 0) { 
      return removeFirst(); 
     } 
     else if (index == size - 1) { 
      return removeLast(); 
     } 
     else { 
      Node<E> previous = head; 

      for (int i = 1; i < index; i++) { 
      previous = previous.next; 
      } 

      Node<E> current = previous.next; 
      previous.next = current.next; 
      size--; 
      return true; 
     } 
     } 

    @Override 
     public boolean removeFirst() 
     { 
      if (size == 0) { 
      return false; 
     } 
     else { 
      Node<E> temp = head; 
      head = head.next; 
      size--; 
      if (head == null) { 
      tail = null; 
      } 
      return true; 
     } 
     } 

    @Override 
     public boolean removeLast() 
     { 
     if (size == 0) { 
       return false; 
     } 
     else if (size == 1) { 
      Node<E> temp = head; 
      head = tail = null; 
      size = 0; 
      return true; 
     } 
     else { 
      Node<E> current = head; 

      for (int i = 0; i < size - 2; i++) { 
      current = current.next; 
      } 

      Node<E> temp = tail; 
      tail = current; 
      tail.next = null; 
      size--; 
      return true; 
     } 
    } 
    @Override 
     public boolean set(int index, E e) 
     { 
      if (index < 0 || index > size - 1) 
      return false; 

      Node<E> current = head; 
      for (int i = 0; i < index; i++) 
      current = current.next; 

      E temp = current.element; 
      current.element = e; 

      return true; 
     } 

    @Override 
    public String toString() 
    { 
     StringBuilder result = new StringBuilder("["); 

     Node<E> current = head; 
     for (int i = 0; i < size; i++) { 
      result.append(current.element); 
      current = current.next; 
      if (current != null) { 
      result.append(", "); // Separate two elements with a comma 
      } 
      else { 
      result.append("]"); // Insert the closing ] in the string 
      } 
     } 

     return result.toString(); 
    } 

     public String toStringBack() 
    { 
     StringBuilder result = new StringBuilder("["); 

     Node<E> current = tail; 
     for (int i = 0; i < size; i++) { 
      result.append(current.element); 
      current = current.previous; 
      if (current != null) { 
      result.append(", "); // Separate two elements with a comma 
      } 
      else { 
      result.append("]"); // Insert the closing ] in the string 
      } 
     } 

     return result.toString(); 
    } 

    public void add(int index, E e) 
    { 

     if (index ==0) { 
      addFirst(e);// The new node is the only node in list 
     } 
     else if(index > size) 
     { 
      addLast(e);//The index location was bigger than size 
     } 
     else 
     { 
      Node<E> current = getAtIndex(index-1); 
      Node<E> temp = current.next; 
      current.next = new Node<E>(e); 
      (current.next).next = temp; 
      size++; 
     } 

    } 

    public void addFirst(E e) 
    { 
     Node<E> newNode = new Node<E>(e); // Create a new node 
     newNode.next = head; // link the new node with the head 
     head = newNode; // head points to the new node 
     size++; // Increase list size 

     if (tail == null) // the new node is the only node in list 
      tail = head; 

    } 

    public void addLast(E e) 
    { 
     Node<E> newNode = new Node<E>(e); // Create a new for element e 

     if (tail == null) { 
      head = tail = newNode; // The new node is the only node in list 
     } 
     else { 
      tail.next = newNode; // Link the new with the last node 
      tail = tail.next; // tail now points to the last node 
     } 

     size++; 
    } 

    protected Node<E> getAtIndex(int index) 
    { 
     int count; 
     if (index < 0 || index > size - 1) 
     return null; 
     Node<E> temp = null; 
     if(index <= (size/2)) 
      { 
      count = -1; 
      temp = head; 
      while(++count <= index){ 
       temp = temp.next; 
       } 
      } 
      else if (index > (size/2)) 
      { 
      count = size; 
      temp = tail; 
      while(--count >= index){ 
       temp = temp.previous; //--> this is Where the null pointer exception occurs 
      } 
     } 
      return temp; 
    } 
    private static class Node<E>{ 
     E element; 
     Node<E> next; 
     Node<E> previous; 
     public Node(E element){ 
      this.element = element; 
      next = null; 
      previous = null; 
     } 
     } 
} 

add(int index、E e)メソッドを実行すると、getAtメソッドでnullポインタ例外が発生しますIndext()メソッド。私はまた、現在の場所を変更したときにadd(E e)メソッドを正しく追加することに問題がありました。メソッドは、私がそれらを使用するために必要とされているすべてです。イテレータの使い方は良いとは言えません。私は、Blujをコンパイラとして使用しています。あなたが持っている質問を教えてください。

はあなたに

EDITありがとう:私はNULLポインタ例外が何であるかを知っている、これはヌルとして戻って来ている理由は、私が把握することはできません。ノード「現在」は終点を指し示し、インデックスされた位置に達するまでリストに戻る。これは、リストの最後の半分にノードを追加しようとした場合にのみ発生します。エラーがtemp = temp.previous; でスローされるスタックトレースは次のとおりです。TestMyDoublyLinkedList.mainでMyDoublyLinkedList.add(MyDoublyLinkedList.java:289)でMyDoublyLinkedList.getAtIndex(MyDoublyLinkedList.java:345)(TestMyDoublyLinkedList.java:50で のjava.lang.NullPointerException )

編集:私は問題を把握しました。 add()およびgetAtIndex関数が正しく実行されています。 add()方法で挿入するとき、私はNULLポインタ例外を投げている機能は、罰金最初のいくつかのノードを介して位置に8.サイクルを、それを投げているテストを実行しますが、ノード8

+2

[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – DimaSan

+0

スタックトレースを追加できますか?それは多くを助ける –

答えて

0

で死ぬとき、あなたはpreviousを割り当てることはありませんすべてのノードではnullです。

+0

しかし、それはなぜ.nextがうまくいくのか説明しません。これらはNodeクラスで定義され、Nullポインタを投げるセクションの上のセクションはgetAtIndexメソッドでも定義しなかった.nextを持ちます。 – BlankKitOkami

+0

挿入するときは、次は割り当てますが前は割り当てません。 – eduyayo

関連する問題