java - 为链表对象编写 equals() 方法

标签 java linked-list equals

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

通过运行另一个名为 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;
}

关于java - 为链表对象编写 equals() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844026/

相关文章:

java - Java 中的对象标识和相等性

java - java 中两个对象具有相同哈希值但 equals 方法返回 false 的示例

java - 按类别将一个集合拆分为多个集合

c - 从链表中删除第一个节点后结果不正确

java - 链表删除错误

c# - 链表与列表<T>

java - Spring Web Flow模型绑定(bind)到不同的请求参数

java - 在不同线程中抛出的异常会导致主线程崩溃吗?

java - 如何将 java NetBeans 中 if 条件的值变为 true?

java - 为什么 Map 的 containsKey() 只调用 hashCode()?