代码在节点中存储了错误的数据,而之前的测试在我的双向链表中运行正常,并给我一个 OutOfMemoryError
Code storing wrong data in nodes, when previous test's worked right in my Doubly-Linked-List, and giving me an OutOfMemoryError
所以我已经开始完成这周的作业,并从最重要的方法开始,即 insertbefore() 方法。这种方法似乎对每个测试都有效,直到测试在双向链表中的第 2 点插入。之前的测试(在第 1 点插入)运行良好并为我提供了预期的输出,但对于此测试,它为我提供了错误的输出并导致 OutOfMemoryError...
我很困惑这是怎么发生的,以及为什么代码会出乎意料地运行。我在 DoublyLinkedLists 方面也有点不确定,并且已经在这个程序上苦苦挣扎了很长一段时间,任何帮助都将不胜感激
下面是我的代码:
public void insertBefore( int pos, T data )
{
if(isEmpty()){
DLLNode current = new DLLNode(data,null,null);
head = current;
tail = current;
}
else if(pos<=0){
DLLNode current = new DLLNode(data,null,head);
head.prev = current;
current.next = head;
current.prev = null;
head = current;
}
else if(pos>=count){
DLLNode current = new DLLNode(data,tail,null);
tail.next = current;
current.prev = tail;
current.next = null;
tail = current;
}
else{
DLLNode current = new DLLNode(data,null,null);
int i=1;
DLLNode posZero = head;
while(i<count){
if(i==pos){
DLLNode tmp = head.next;
posZero.next = current;
current.prev = head;
current.next = tmp;
tmp.prev = current;
}
posZero = posZero.next;
i++;
}
}
System.out.println();
displayNodeList();
count++;
return;
}
看看这段代码...
DLLNode posZero = head;
while(i<count){
if(i==pos){
DLLNode tmp = head.next;
"tmp"永远是头的下一个,换句话说,就是第二个元素。这样,您在 toString 中创建了一个无限循环,因为插入的元素指向第二个元素作为下一个,然后它将再次指向插入的元素,etc.etc.
头 -> 1. -> 2. -> 1. -> 2. -> 1. -> 2. -> 等等
这就是您遇到内存不足错误的原因:您的 StringBuilder 只是不断地增长、增长和增长……直到没有内存为止。对你来说幸运的是,你没有为此使用递归,因为那会让你得到一个 Whosebug ......好吧,可能不是真的更好;-)
作为一个建议:如果您 a) 将每个测试放在一个单独的方法中并且 b) 使用某种断言框架,例如 Hamcrest、AssertJ 或 Truth,您的测试将更具可读性。
所以我已经开始完成这周的作业,并从最重要的方法开始,即 insertbefore() 方法。这种方法似乎对每个测试都有效,直到测试在双向链表中的第 2 点插入。之前的测试(在第 1 点插入)运行良好并为我提供了预期的输出,但对于此测试,它为我提供了错误的输出并导致 OutOfMemoryError...
我很困惑这是怎么发生的,以及为什么代码会出乎意料地运行。我在 DoublyLinkedLists 方面也有点不确定,并且已经在这个程序上苦苦挣扎了很长一段时间,任何帮助都将不胜感激
下面是我的代码:
public void insertBefore( int pos, T data )
{
if(isEmpty()){
DLLNode current = new DLLNode(data,null,null);
head = current;
tail = current;
}
else if(pos<=0){
DLLNode current = new DLLNode(data,null,head);
head.prev = current;
current.next = head;
current.prev = null;
head = current;
}
else if(pos>=count){
DLLNode current = new DLLNode(data,tail,null);
tail.next = current;
current.prev = tail;
current.next = null;
tail = current;
}
else{
DLLNode current = new DLLNode(data,null,null);
int i=1;
DLLNode posZero = head;
while(i<count){
if(i==pos){
DLLNode tmp = head.next;
posZero.next = current;
current.prev = head;
current.next = tmp;
tmp.prev = current;
}
posZero = posZero.next;
i++;
}
}
System.out.println();
displayNodeList();
count++;
return;
}
看看这段代码...
DLLNode posZero = head;
while(i<count){
if(i==pos){
DLLNode tmp = head.next;
"tmp"永远是头的下一个,换句话说,就是第二个元素。这样,您在 toString 中创建了一个无限循环,因为插入的元素指向第二个元素作为下一个,然后它将再次指向插入的元素,etc.etc.
头 -> 1. -> 2. -> 1. -> 2. -> 1. -> 2. -> 等等
这就是您遇到内存不足错误的原因:您的 StringBuilder 只是不断地增长、增长和增长……直到没有内存为止。对你来说幸运的是,你没有为此使用递归,因为那会让你得到一个 Whosebug ......好吧,可能不是真的更好;-)
作为一个建议:如果您 a) 将每个测试放在一个单独的方法中并且 b) 使用某种断言框架,例如 Hamcrest、AssertJ 或 Truth,您的测试将更具可读性。