为链表对象编写一个 equals() 方法

Writing an equals() method for linked list object

我正在构建名为 LString 的 class 中的 equals(Object other) 方法时遇到问题。 class 包含一个名为 LString 的对象,它从链表中构建字符串,以及一些其他方法。我正在研究两种方法,compareTo()equals()

class 由另一个名为 LStringTest 的文件 运行 测试,该文件输出此错误消息:

Running compareTo and equals tests (18 tests)
Starting tests: .E.........E...E.E
Time: 0.426

There were 4 failures:
1) t22aTestEquals[0](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
2) t22aTestEquals[5](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
3) t22aTestEquals[7](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
4) t22aTestEquals[8](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more

Test Failed! (4 of 18 tests failed.)

Test failures: abandoning other phases.

我相信我 compareTo()equals() 是正确的,但我找不到错误的根源。

这是我的代码:

import java.io.*;
import java.util.*;

public class LString    {

     node   front;
     int size;

    //Creating a node class
     private    class   node {
          char data;
          node next;

          public    node (){
          }

          public    node    (char   newData){
                this.data = newData;
          }

          public    node    (char   newData,    node newNext){
                this.data = newData;
                this.next = newNext;
          }


     }
    //Constructors
     public LString(){
          this.size =   0;
          this.front =  null;
     }
     public LString(String original)    {
          this.size = original.length();
          if (original.length() > 0){

              this.front =  new node(original.charAt(0));
              node curr = this.front;

              for   (int i =1; i <  original.length(); i++) {
                    curr.next = new node(original.charAt(i));
                    curr = curr.next;
              }
          }



     }

   // Length method, returns the length of LString
     public int length()    {
        return this.size;
    }
    // compareTo method, compares this LString to anotherLString, returns 0 if equal,
   // -1 if lexicogrpahically less, and 1 if lexicographically greater
    public int compareTo(LString anotherLString)    {
      int len1 = length();
      int len2 = anotherLString.length();
      int lim = Math.min(len1, len2);

      node cn1 = front;
      node cn2 = anotherLString.front;

      int k = 0;
      while (k < lim) {
         char c1 = cn1.data;
         char c2 = cn2.data;
         if (c1 != c2) {
            return c1-c2;
         }
         k++;
         cn1 = cn1.next;
         cn2 = cn2.next;
      }
      return len1 - len2;
    }
   // a boolean equals method that returns true if LString and other are the same, false if not
    public boolean  equals(Object other)    {
      if (this == other) {
         return true;
      }
        if  (other instanceof LString) {
         LString otherLString   = (LString)other;
         int n = length();
         if (n == otherLString.length()) {
            node n1 = front;
            node n2 = otherLString.front;
            while (n1 != null) {
               if (n1.data != n2.data) {
                  return false;
               }
            }
            return true;
         }
      }

      return false;
   }
}

多谢指教,努力学习java。

您的 equals 方法包含无限循环。您的 while 循环检查是否 n1 != null,但是您在循环中的任何地方都没有更改 n1:

的值
    while (n1 != null) {
        if (n1.data != n2.data) {
            return false;
        }
    }

检查每个值后,您需要在列表中向下移动节点:

while (n1 != null) {
    if (n1.data != n2.data) {
        return false;
    }
    n1 = n1.next;
    n2 = n2.next;
}