Java lang断言错误的字符串列表

发布于 2025-02-02 01:44:50 字数 2964 浏览 1 评论 0原文

我会收到以下错误,以检查两个字符串列表是否相等:

java.lang.assertionerror: 预期:Conslostring@3E57CD70 实际:Conslostring@9A7504C

Conslostring是一个具有值的列表的类 而mtlostring是一个用于空列表的类。 我需要覆盖平等或哈希码吗?

这是代码:

// to represent a list of Strings
interface ILoString {
  // combine all Strings in this list into one
  String combine();
  ILoString findAndReplace(String s1, String s2);
}

// to represent an empty list of Strings
class MtLoString implements ILoString {
  MtLoString(){}

  // combine all Strings in this list into one
  public String combine() {
    return "";
  }

  @Override
  public ILoString findAndReplace(String s1, String s2) {
    return new MtLoString();
  }
}



// to represent a nonempty list of Strings
class ConsLoString implements ILoString {
  String first;
  ILoString rest;

  ConsLoString(String first, ILoString rest) {
    this.first = first;
    this.rest = rest;
  }
    
    /*
     TEMPLATE
     FIELDS:
     ... this.first ...         -- String
     ... this.rest ...          -- ILoString
     
     METHODS
     ... this.combine() ...     -- String
     
     METHODS FOR FIELDS
     ... this.first.concat(String) ...        -- String
     ... this.first.compareTo(String) ...     -- int
     ... this.rest.combine() ...              -- String
     
     */

  // combine all Strings in this list into one
  public String combine() {
    return this.first.concat(this.rest.combine());
  }

  // takes in two Strings as arguments.
  // It produces a list where all occurrences of the first given
  // String are replaced by the second given String.
  @Override
  public ILoString findAndReplace(String s1, String s2) {
    // if the strings are the same
    // well then the list is the same; return the same list while
    // still traversing
    if (s1.compareTo(s2) == 0) {
      return new ConsLoString(s2, this.rest.findAndReplace(this.first, s2));
    } else {
      return new ConsLoString(s1, this.rest.findAndReplace(this.first, s2));
    }
  }
}

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ExamplesStrings{


  ILoString mary = new ConsLoString("Mary ",
          new ConsLoString("had ",
                  new ConsLoString("a ",
                          new ConsLoString("little ",
                                  new ConsLoString("lamb.",
                                          new MtLoString())))));
  ILoString mary1 = new ConsLoString("Mary ",
          new ConsLoString("a ",
                  new ConsLoString("a ",
                          new ConsLoString("little ",
                                  new ConsLoString("lamb.",
                                          new MtLoString())))));

  @Test
  public void testFindAndReplace() {
    assertEquals(mary.findAndReplace("had", "a"), mary1);
    assertEquals(mary.findAndReplace("monkey", "deez"),
            mary);
  }
}

I am getting the following error for checking if two Lists of Strings are equal:

java.lang.AssertionError:
Expected :ConsLoString@3e57cd70
Actual :ConsLoString@9a7504c

ConsLoString is a class for lists that have values,
while MtLoString is a class for empty lists.
Do I need to override equals or hashcode?

here is the code:

// to represent a list of Strings
interface ILoString {
  // combine all Strings in this list into one
  String combine();
  ILoString findAndReplace(String s1, String s2);
}

// to represent an empty list of Strings
class MtLoString implements ILoString {
  MtLoString(){}

  // combine all Strings in this list into one
  public String combine() {
    return "";
  }

  @Override
  public ILoString findAndReplace(String s1, String s2) {
    return new MtLoString();
  }
}



// to represent a nonempty list of Strings
class ConsLoString implements ILoString {
  String first;
  ILoString rest;

  ConsLoString(String first, ILoString rest) {
    this.first = first;
    this.rest = rest;
  }
    
    /*
     TEMPLATE
     FIELDS:
     ... this.first ...         -- String
     ... this.rest ...          -- ILoString
     
     METHODS
     ... this.combine() ...     -- String
     
     METHODS FOR FIELDS
     ... this.first.concat(String) ...        -- String
     ... this.first.compareTo(String) ...     -- int
     ... this.rest.combine() ...              -- String
     
     */

  // combine all Strings in this list into one
  public String combine() {
    return this.first.concat(this.rest.combine());
  }

  // takes in two Strings as arguments.
  // It produces a list where all occurrences of the first given
  // String are replaced by the second given String.
  @Override
  public ILoString findAndReplace(String s1, String s2) {
    // if the strings are the same
    // well then the list is the same; return the same list while
    // still traversing
    if (s1.compareTo(s2) == 0) {
      return new ConsLoString(s2, this.rest.findAndReplace(this.first, s2));
    } else {
      return new ConsLoString(s1, this.rest.findAndReplace(this.first, s2));
    }
  }
}

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ExamplesStrings{


  ILoString mary = new ConsLoString("Mary ",
          new ConsLoString("had ",
                  new ConsLoString("a ",
                          new ConsLoString("little ",
                                  new ConsLoString("lamb.",
                                          new MtLoString())))));
  ILoString mary1 = new ConsLoString("Mary ",
          new ConsLoString("a ",
                  new ConsLoString("a ",
                          new ConsLoString("little ",
                                  new ConsLoString("lamb.",
                                          new MtLoString())))));

  @Test
  public void testFindAndReplace() {
    assertEquals(mary.findAndReplace("had", "a"), mary1);
    assertEquals(mary.findAndReplace("monkey", "deez"),
            mary);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文