Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 上次测试时,在节点中存储错误数据的代码';s在我的双链接列表中工作正常,给了我一个OutOfMemoryError_Java_Junit_Stack_Doubly Linked List - Fatal编程技术网

Java 上次测试时,在节点中存储错误数据的代码';s在我的双链接列表中工作正常,给了我一个OutOfMemoryError

Java 上次测试时,在节点中存储错误数据的代码';s在我的双链接列表中工作正常,给了我一个OutOfMemoryError,java,junit,stack,doubly-linked-list,Java,Junit,Stack,Doubly Linked List,所以我开始了这周的作业,并开始了最重要的实现方法,insertbefore()方法。这种方法似乎对每个测试都很有效,直到在双链表的第2点进行插入测试为止。上一个测试(在点1插入)运行良好,并提供了预期的输出,但对于此测试,它提供了错误的输出,并导致OutOfMemoryError 我很困惑这是怎么发生的,以及为什么代码会出人意料地运行。我对双链接列表方面也有点不确定,并且在这个项目上奋斗了相当长的一段时间,任何帮助都将不胜感激 下面是我的代码: public void insertBefore(

所以我开始了这周的作业,并开始了最重要的实现方法,insertbefore()方法。这种方法似乎对每个测试都很有效,直到在双链表的第2点进行插入测试为止。上一个测试(在点1插入)运行良好,并提供了预期的输出,但对于此测试,它提供了错误的输出,并导致OutOfMemoryError

我很困惑这是怎么发生的,以及为什么代码会出人意料地运行。我对双链接列表方面也有点不确定,并且在这个项目上奋斗了相当长的一段时间,任何帮助都将不胜感激

下面是我的代码:

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;
}
public void insertBefore(int pos,T data)
{
if(isEmpty()){
DLLNode current=新的DLLNode(数据,null,null);
水头=电流;
尾=电流;
}
否则如果(位置=计数){
DLLNode current=新的DLLNode(数据、尾部、空);
tail.next=当前;
current.prev=尾部;
current.next=null;
尾=电流;
}  
否则{
DLLNode current=新的DLLNode(数据,null,null);
int i=1;
DLLNode posZero=磁头;

虽然(我看看这个代码

    DLLNode posZero = head;
    while(i<count){
        if(i==pos){
            DLLNode tmp = head.next;
DLLNode posZero=head;
而(i 1.->2.->1.->2.->1.->2.->等

这就是为什么会出现内存不足错误的原因:StringBuilder只会不断增长…直到没有内存。幸运的是,您没有为此使用递归,因为这会导致堆栈溢出…好的,可能不是更好;-)


作为一个建议:如果你能将每个测试放在一个单独的方法中,b)使用某种断言框架,如Hamcrest、AssertJ或Truth。

您是一个救命恩人,谢谢,刚刚将代码更改为DLLNode tmp=posZero。接下来,它现在运行得很好!就测试而言,它们是我们讲师给我们的测试,不允许使用被改变或改变,好吧。然后我什么也没说。除了记住这些可能不是写断言的最完美的方式;-)不,如果你的问题是“关于作业”的,你不能删除它们。在发布作业之前,请仔细考虑。需要删除多余的代码,以便它不会被使用或损坏,因此我会将其编辑到发生错误的代码位置