title: Add(index, element) method
body: <p>I am implementing a method add(int index, E element) that is supposed to insert the specified element at the specified index in a list and shift the element currently at that position and any subsequent elements to the right. Does anyone know why my method does not work when my code is:</p>
  
  <pre><code>        newNode.setNext(temp.getNext());
          newNode.setPrev(temp);
          newNode.getNext().setPrev(newNode);
          temp.setNext(newNode);
  </code></pre>
  
  <p>at the end of the method but works when I have only</p>
  
  <pre><code>        newNode.setNext(temp.getNext());
          temp.setNext(newNode);
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>public class DoubleLinkedList&lt;E&gt; implements IDoubleLinkedList&lt;E&gt; {
  
  DLLNode head;
  DLLNode tail;
  int size = 0;
  @Override
  public void add(int index, E element) throws IndexOutOfBoundsException {
      if (index &gt; size) {
          throw new IndexOutOfBoundsException();
      }
      if (index &lt; 0) {
          throw new IndexOutOfBoundsException();
      }
      if (head == null) {
          head = new DLLNode(element);
          tail = head;
      }
      else if (index == 0) {
          DLLNode temp = new DLLNode(element);
          temp.setNext(head);
          head = temp;
      } else {
          DLLNode temp = head;
          for (int i = 1; i &lt; index; i++) {
              temp = temp.getNext();
          }
          DLLNode newNode = new DLLNode(element);
          newNode.setNext(temp.getNext());
          newNode.setPrev(temp);
          newNode.getNext().setPrev(newNode);
          temp.setNext(newNode);
      }
      size ++;
  }
  </code></pre>
  
  <p>Node class for my DoublyLinkedlist:</p>
  
  <pre><code>public class DLLNode&lt;E&gt; {
  private DLLNode&lt;E&gt; next;
  private DLLNode&lt;E&gt; prev;
  private E element;
  
  public DLLNode(E element){
      this.element=element;
      prev=null;
      next=null;
  }
  public DLLNode(E element, DLLNode prev, DLLNode next) {
      this.element=element;
      this.prev=prev;
      this.next=next;
  }
  public E getData(){
      return element;
  }
  
  public void setData(E element){
      this.element=element;
  }
  
  public DLLNode getPrev(){
      return prev;
  }
  
  public DLLNode getNext(){
      return next;
  }
  
  public void setPrev(DLLNode where){
      prev=where;
  }
  
  public void setNext(DLLNode where){
      next=where;
  }}
  </code></pre>