title: equals method for DoublyLinkedList
body: <p>I got this assignment to create an equals method for doubly linked list. So far I got the code I posted below. It passes some of the tests, but not all. The failures do have the same expected value as the actual value, but I still get some AssertionErrors.</p>
  
  <p>Ive been messing and trying to change things for quite some time now, but I cant seem to figure it out on my own. Nor can I find any good examples on the internet. I've tried using Eclipse to generate the equals method, but that too does not pass all the tests.</p>
  
  <p>I know you do not give away free answers here, but could someone maybe point to the mistake in the code?</p>
  
  <pre><code>    /**
       * This method should return true iff the values of this list 
       * and that are identical and in the same order.
       * @param that list to compare this to.
       * @return true iff the values are identical and in the same order
       */
      public boolean equals(Object that) {
        if (that == null)
          return false;
        if (!(that instanceof DoublyLinkedList) )
      return false;
  
    DoublyLinkedList&lt;E&gt; other = (DoublyLinkedList&lt;E&gt;) that;
    if (header == null&amp;&amp;other.header != null)
              return false;
          if (trailer == null&amp;&amp;other.trailer != null)
              return false;           
  
    while (header.getNext() != trailer){
        if (!(header.equals(other.header))){
                return false;
        }
                header = header.getNext();
                other.header = other.header.getNext();
       }
          return true;
      }
  </code></pre>
  
  <p>Edit, per request the failed tests en DLL class:</p>
  
  <pre><code>public static class DoublyLinkedList&lt;E&gt; {
      private Node&lt;E&gt; header;
      private Node&lt;E&gt; trailer;
  
      /** 
       * Constructor that creates an empty DLL
       */
      public DoublyLinkedList() {
          this.header = new Node&lt;&gt;(null, null, null);
          this.trailer = new Node&lt;&gt;(null, header, null);
          this.header.setNext(trailer);
      }
  
      /**
       * @return if the list is empty.
       */
      public boolean isEmpty() {
        return this.header.getNext() == this.trailer;
      }
  
      /**
       * @return the first element of the list.
       */
      public E getFirst() {
        if (isEmpty()) return null;
          return this.header.getNext().getElement();
      }
  
      /**
       * @return the last element of the list.
       */
      public E getLast() {
        if (isEmpty()) return null;
          return this.trailer.getPrevious().getElement();
      }
  
  
      /**
       * Adds a new Node to the beginning of the list, 
       * containing the specified value.
       * @param value for the new first node to hold.
       */
      public void addFirst(E element) {
          Node&lt;E&gt; newNode = new Node&lt;&gt;(element, header, header.getNext());
          header.getNext().setPrevious(newNode);
          header.setNext(newNode);
      }
  
      /**
       * This method should return true iff the values of this list 
       * and that are identical and in the same order.
       * @param that list to compare this to.
       * @return true iff the values are identical and in the same order
       */
      public boolean equals(Object that) {
        if (that == null)
          return false;
        if (!(that instanceof DoublyLinkedList) )
      return false;
  
    DoublyLinkedList&lt;E&gt; other = (DoublyLinkedList&lt;E&gt;) that;
    if (header == null&amp;&amp;other.header != null)
              return false;
          if (trailer == null&amp;&amp;other.trailer != null)
              return false;           
  
    while (header.getNext() != trailer){
        if (!(header.equals(other.header))){
                return false;
        }
                header = header.getNext();
                other.header = other.header.getNext();
       }
          return true;
      }
  
  
      /**
       * Simple toString for testing purposes. Please note that solutions that use the
       * .toString() to implement the .equals() method will be rejected.
       */
      public String toString() {
          StringBuilder sb = new StringBuilder();
          sb.append("DoublyLinkedList&lt;");
  
          Node&lt;E&gt; finger = header.getNext();
          while (finger != trailer) {
              sb.append(finger.toString());
              if (finger.getNext() != trailer) {
                  sb.append("-");
              }
              finger = finger.getNext();
          }
  
          sb.append("&gt;");
          return sb.toString();
      }
  }
  </code></pre>
  
  <p>And the tests:</p>
  
  <pre><code>@Test
      public void testEqualsCopy() {
          Solution.DoublyLinkedList&lt;Integer&gt; dll1 = createDLL(2);
          Solution.DoublyLinkedList&lt;Integer&gt; dll2 = createDLL(2);
          assertEquals(dll1, dll2);
      }
          @Test
      // Both lists contain only the entry 42.
      public void testTwoEqualLists() {
          Solution.DoublyLinkedList&lt;Integer&gt; dll1 = new Solution.DoublyLinkedList&lt;&gt;();
          Solution.DoublyLinkedList&lt;Integer&gt; dll2 = new Solution.DoublyLinkedList&lt;&gt;();
          dll1.addFirst(42);
          dll2.addFirst(42);
          assertEquals(dll1, dll2);
      }
  </code></pre>
  
  <p>and the errors: </p>
  
  <pre><code>testEqualsCopy(UTest) failed: 'java.lang.AssertionError: expected: Solution$DoublyLinkedList&lt;DoublyLinkedList&lt;1-0&gt;&gt; but was: Solution$DoublyLinkedList&lt;DoublyLinkedList&lt;1-0&gt;&gt;'
  testTwoEqualLists(UTest) failed: 'java.lang.AssertionError: expected: Solution$DoublyLinkedList&lt;DoublyLinkedList&lt;42&gt;&gt; but was: Solution$DoublyLinkedList&lt;DoublyLinkedList&lt;42&gt;&gt;'
  </code></pre>