【发布时间】:2017-07-29 07:56:01
【问题描述】:
在我的linkedList 类中,我的 toString 得到了奇怪的输出。
我不能使用任何方法,只能使用字符串连接。处理这个问题的方法非常有限。
代码如下:
@Override
public String toString() {
if (head == null) {
return "";
}
String result = "";
Node curr = head;
while (curr.next != null) {
curr = curr.next;
result += curr.data + ", ";
}
return result;
}
我写了一个 JUnit 测试:
assetsEquals(
"8,6,7,5,3,0,9, " + "9,0,3,5,7,6,8", dataTest.noOrderList().toString());
并且 noOrderList().toString() 来自:
public static noOrderList<Integer> noOrderList() {
return makeListInNoOrder(new Integer[]{ 8, 6, 7, 5, 3, 0, 9, 9, 0, 3, 5, 7, 6, 8});
当我运行测试时,我得到:
expected:<... 3, 0, 9, 9, 0, 3[]> but was: <... 3, 0, 9, 9, 0, 3[, ]>
这是造成这个的原因,在[,]我如何消除那个逗号?
谢谢
【问题讨论】:
-
列表 "8, 6, 7, 5, 3, 0, 9, 9, 0, 3, 5, 7, 6, 8" 如何返回 "3, 0、9、9、0、3"?
-
@Steve Smith:我想输出只是为了更好的可读性而缩短了?但他/她不应该使用对称列表进行测试,因为该单元测试可能会隐藏意外创建的相反顺序的列表。
-
@yre ,您实际上是在列表末尾得到“[, ]”,还是只是一个逗号?
标签: java junit linked-list tostring