java - 我的交错链接列表功能没有更改所需的列表
问题描述
我试图交错两个列表,这样:
list1 以 {a1,a2,a3,a4,a5} 开头 list2 以 {b1,b2,b3,b4,b5} 开头
我希望他们成为 {a1,b2,a3,b4,a5} {b1,a2,b3,a4,b5}
这是我的代码:
public void interleave(A3LinkedList other) {
A3Node thisCurrent = this.head;
A3Node otherCurrent = other.head;
int count = 1;
while(count <= this.length){
System.out.println("List1 current at loop "+ count + ": " + thisCurrent); // just to debug
System.out.println("List2 current at loop "+ count + ": " + otherCurrent);
A3Node tempThis = thisCurrent;
A3Node tempOther = otherCurrent;
//if(count%2 == 0){ //if count is even, aka every second object in list
thisCurrent = tempOther; // makes thisCurrent same as otherCurrent
otherCurrent = tempThis; // makes otherCurrent same as temp saved
//}
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
count ++;
}
}
while 循环中 println 方法的输出显示节点交换正确,但另一个测试程序的最终结果显示列表根本没有改变。
while循环内部的输出:
List1 current at loop 1: A
List2 current at loop 1: L
List1 current at loop 2: M
List2 current at loop 2: B
List1 current at loop 3: C
List2 current at loop 3: N
List1 current at loop 4: O
List2 current at loop 4: D
List1 current at loop 5: E
List2 current at loop 5: P
List1 current at loop 6: Q
List2 current at loop 6: F
List1 current at loop 7: G
List2 current at loop 7: R
测试器的输出:{ABCDEFG} {LMNOPQR} 测试失败:第 203 行的 testInterleave 失败的测试:第 204 行的 testInterleave
可以看到,测试器的输出不是它应该的,它没有通过测试。为什么?
解决方案
推荐阅读
- scala - 在 flink 测试用例中使用模拟类时无法加载用户类:org.mockito.codegen.RestUtil
- windows - 如何使电子应用程序响应 DPI 缩放?
- google-sheets - 如何自动更改句子以添加列表中的单词(Google 表格或 Excel)
- python - FreeFem++ movemesh 不变换网格
- c - 用于比较 C 中数字的类似 strcmp 的接口
- html - 如果文本长于 20 个字符,则在数据表列中显示更多/更少选项
- python - 作为可选参数的函数 python
- swift - SwiftUI 导航标题重叠
- java - 在 java 中使用 mailGun API 将 InputStreamSource 作为附件发送
- mongodb - 如何在 mongodb 的内部字段上按升序排序?