title: LinkedList indexOf method check
body: <p>i am currently building a linked list from scratch and everything seems to be going okay except on my test driver when i try and get an index of a certain object it gives me a value that seems to be 2 values off, (should be 3, its 5) </p>
  
  <p><strong>Test Driver</strong></p>
  
  <pre><code>public static void main ( )
  {   List&lt;String&gt; friends = new LinkedList &lt;String&gt; ();
  
      System.out.println (" Testing problem 1");
  friends.add ("joe");
  friends.add ("mary");
  friends.add ("jim");
  friends.add ("joe");                            // Lists may contain duplicate elements
  friends.add (2, "sally");                       // Insert at position 2
  System.out.println (friends.get(3));            // Should be jim
  friends.remove (0);
  System.out.println (friends.indexOf ("joe"));   // Should be 3
  String sal = new String ("sa" + "lly");         
  if (! friends.contains(sal))                    // "sally"
      System.err.println ("Not correct");
  </code></pre>
  
  <p><strong>My indexOf method</strong></p>
  
  <pre><code> /**
   * 
   * @returns index value, -1 if object was not found
   */
  public int indexOf(Object obj) {
  
       int index = 0;
      Node&lt;E&gt; current = head.next;
      while (current != null) {
          if (current.equals( obj)) {
              return index;
          }
  
          index++;
          current = current.next;
      }
      if(index == size &amp;&amp; obj == null){
          return -1;
      }
      else{
          return index;
      }
  }
  </code></pre>
  
  <p><strong>My add(E value) method</strong></p>
  
  <pre><code>public void add(E value){
      Node&lt;E&gt; temp = new Node&lt;E&gt;(value,tail,tail.previous);
      tail.previous = temp;
      temp.previous.next = temp;
      size++;
  
  }
  </code></pre>
  
  <p><strong>My add(int index, E value) method</strong></p>
  
  <pre><code>public void add(int index, E value) {
     setRef(index);
    Node&lt;E&gt; temp = new Node&lt;E&gt;(value,ref,ref.previous);
    temp.previous.next = temp;
    ref.previous = temp;
    size++;
  }
  </code></pre>
  
  <p>any idea why i may be getting an index of 5 when it should be 3? </p>
  
  <p><strong>For those who want full source</strong></p>
  
  <p><strong>LinkedList.java</strong></p>
  
  <pre><code>package list;
  
  
  public class LinkedList&lt;E&gt; implements List&lt;E&gt;
  {
      //size of list
      int size = 0;
      Node&lt;E&gt; head;
      Node&lt;E&gt; tail;
      Node&lt;E&gt;ref;
      public LinkedList(){
          head = new Node&lt;E&gt;(null,null,null);
          tail = new Node&lt;E&gt;(null,null,head);
          head.next = tail;
      }
  
      public void add(E value){
  
          Node&lt;E&gt; temp = new Node&lt;E&gt;(value,tail,tail.previous);
          tail.previous = temp;
          temp.previous.next = temp;
          size++;
  
      }
  
      public E get(int index){
       setRef(index);
       return ref.value;
      }
  
      public E set(int index, E value){
          setRef(index);
          E result = ref.value;
          ref.value = value;
          return result;
      }
  
      public void add(int index, E value) {
         setRef(index);
        Node&lt;E&gt; temp = new Node&lt;E&gt;(value,ref,ref.previous);
        temp.previous.next = temp;
        ref.previous = temp;
        size++;
      }
  
  
      public int size(){
          return size;
      }
  
      public E remove(int index){
        setRef(index);
          E result = ref.next.value;
          ref.previous.next = ref.next;
          ref.next.previous = ref.previous;
          size --;
          return result;
      }
  
      public void clear(){
         size = 0;
         head.next = tail;
         tail.previous = head;
         ref = null;
  
      }
  
      /**
       * @returns true if size of list is equal to 0
       */
      public boolean isEmpty(){
          if(size == 0 ){
              return true;
          }
          else{
              return false;
          }
      }
  
      private void setRef(int index){
      ref = head.next;
      for(int i = 0; i &lt; index; i++){
      ref = ref.next;
      }
  
      }
  
      /**
       * 
       * @returns index value, -1 if object was not found
       */
      public int indexOf(Object obj) {
  
           int index = 0;
          Node&lt;E&gt; current = head.next;
          while (current != null) {
              if (current.equals( obj)) {
                  return index;
              }
  
              index++;
              current = current.next;
          }
          if(index == size &amp;&amp; obj == null){
              return -1;
          }
          else{
              return index;
          }
      }
  
      /**
       * @returns true if list contains Object obj
       */
      public boolean contains(Object obj){
          boolean isTrue;
          if(!(this.indexOf(obj) == -1)){
              isTrue = true;
          }
          else{
              isTrue = false;
          }
          return isTrue;
  
      }
  
      public String toString() {
          String result = "[";
          Node&lt;E&gt; current = head;
          while(current.next != null){
  
              current = current.next;
              result += current.value + ", ";
  
          }
          return   result += "]";
      }
  
      public RefIterator&lt;E&gt; Riterator(){
      return new RefIterator&lt;E&gt;(this);
      }
  
      public ArrayIterator&lt;E&gt;iterator(){
      return new ArrayIterator&lt;E&gt;(this);
      }
      public  ListIterator&lt;E&gt; listIterator(){
      return new RefListIterator&lt;E&gt;(this);
      }
  
      public  ListIterator&lt;E&gt; listIterator(int start){
      return new RefListIterator&lt;E&gt;(this,start);
      }
  
      }
  </code></pre>
  
  <p><strong>List.java</strong></p>
  
  <pre><code>public interface List&lt;E&gt; 
  {
      /**
       * @return the size of this list
       */
      int size(); // all methods in interfaces must be public
  
      /**
       * Clear this list
       */
      void clear();
  
      /**
       *@return the value at a given position by index 
       */
      E get(int index); // index must be in bounds -- check for that later
  
      /**
       * @returns the value that is being replaced
       */
      E set(int index, E value);
  
      /**
       * Insert a given value at the position index
       */
      void add(int index, E value);
  
      /**
       * Inserts a given value at the end of index
       */
      void add(E value);
  
      /**
       * @returns true if List is empty
       */
      boolean isEmpty();
  
      /**
       * removes value at the given index
       * @return value being removed
       */
      E remove(int index);
  
      int indexOf(Object obj);
  
      boolean contains(Object obj);
  
      String toString();
  
      /**
       * @return refrence to the iterator
       */
      ArrayIterator&lt;E&gt;iterator();
  
      RefIterator&lt;E&gt; Riterator();
  
      ListIterator&lt;E&gt; listIterator();
  
      ListIterator&lt;E&gt; listIterator(int start);
  
  }
  </code></pre>
  
  <p><strong>Test Driver</strong></p>
  
  <pre><code>    package list;
  import list.*;
  
  /**
   * Lab 3
   * Test methods added to the List interface
   * 
   * @author (sdb) 
   * @version (Sep 2015)
   */
  public class DriverLab01PM
  {
  /**
   *  This main method tests the List classes
   *  for lab 1, Data Structures and Algorithms
   */
      public static void main ( )
      {   List&lt;String&gt; friends = new LinkedList &lt;String&gt; ();
  
          System.out.println (" Testing problem 1");
      friends.add ("joe");
      friends.add ("mary");
      friends.add ("jim");
      friends.add ("joe");                            // Lists may contain duplicate elements
      friends.add (2, "sally");                       // Insert at position 2
      System.out.println (friends.get(3));            // Should be jim
      friends.remove (0);
      System.out.println (friends.indexOf ("joe"));   // Should be 3
      String sal = new String ("sa" + "lly");         
      if (! friends.contains(sal))                    // "sally"
          System.err.println ("Not correct");
  
  
  //////////// Uncomment the following when ready for problem 2
          System.out.println ("\n\n Testing problem 2");   
          System.out.println (friends);                   // [mary, sally, jim joe]
          List &lt;String&gt; enemies = null;
          System.out.println (enemies);
          enemies = new ArrayList&lt;String&gt; ();
          System.out.println (enemies);
          enemies.add ("mike");
          enemies.add ("mick");
          System.out.println (enemies);
          if (! enemies.contains ("mike"))
              System.err.println ("Not correct");
          if (enemies.contains ("Mike"))
              System.err.println ("Not correct");
  
  // //////////// Uncomment the following when ready for problem 3
      System.out.println ("\n\n Testing problem 3");
      WordList wordList = new WordList();
      List &lt;String&gt; words = wordList.getWordList();
      System.out.println (words.indexOf ("jack"));        // Should be 51595
      if (!words.contains ("zoo"))
          System.err.println ("Error in contains");
      if (words.contains ("foobar"))
          System.err.println ("Error in contains");
  
      wordList = new WordList();
      List &lt;String&gt; moreWords = wordList.getWordList();
      if (!words.equals (moreWords))
          System.err.println ("Error in equals");
      if (!moreWords.equals (words))
          System.err.println ("Error in equals");
      moreWords.add (0, "foobar");
      if (words.equals (moreWords))
          System.err.println ("Error in equals");
      if (moreWords.equals (words))
          System.err.println ("Error in equals");
      moreWords.remove(0);
      moreWords.add ("foobar");
      if (words.equals (moreWords))
          System.err.println ("Error in equals");
      if (moreWords.equals (words))
          System.err.println ("Error in equals");
      String foobar = new String ("foo" + "bar");
      words.add (foobar);                          // "foobar"
      if (!words.equals (moreWords))
          System.err.println ("Error in equals");
      if (!moreWords.equals (words))
          System.err.println ("Error in equals");
      System.out.println ("Testing complete");
    }
  }
  </code></pre>
  
  <p><strong>Node class</strong></p>
  
  <pre><code>    package list;
  
  
  
  public class Node&lt;E&gt; 
  {
      E value;
      Node&lt;E&gt; next;
      Node&lt;E&gt; previous;
      Node(E tempValue, Node&lt;E&gt; tempNext, Node&lt;E&gt; tempPrev){
      this.value = tempValue;
      this.next = tempNext;
      this.previous = tempPrev;
      }
  
  }
  </code></pre>