Java lang断言错误的字符串列表
我会收到以下错误,以检查两个字符串列表是否相等:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论