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

时间:2015-03-03 23:27:16

标签: java linked-list equals

我在一个名为equals(Object other)的建筑物中遇到LString方法时遇到问题。该类包含一个名为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。

1 个答案:

答案 0 :(得分:3)

您的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;
}