简体   繁体   English

为什么要进入循环?

[英]Why am I entering a loop?

It works fine and gives me the correct output until it enters the last object to add to nonEmptyList in the sample() method. 它可以正常工作,并为我提供正确的输出,直到它输入要添加到sample()方法中的nonEmptyList的最后一个对象为止。 I have managed to find out where it is looping, which is in the while loop in the add(item) method. 我设法找出它在哪里循环,在add(item)方法的while循环中。 I can't change the methods or returns, so if anyone could suggest a way I could prevent this infinite loop, that would be appreciated. 我无法更改方法或返回值,因此,如果有人可以建议我可以防止此无限循环的方法,将不胜感激。

public class SampleableListImpl implements SampleableList {

    public int size;
    public Object firstLink = null; //Made a link class to manage each object, this class is the linkedlist (manager) of the object private 
    public ReturnObjectImpl ro;
    int count;
    SampleableListImpl emptyList;
    SampleableListImpl nonEmptyList;

    public ReturnObject add(Object item) { 
        if (firstLink == null){
            firstLink = item;
            size++;
            firstLink.setIndex(0);
        System.out.println("Added linkedlink at 0");
        } else if (firstLink != null){
            Object current = firstLink; 
            while (current.getNextNode() != null){ //LOOPS HERE AFTER sample() sends the last object to be added to nonEmptyList
                current = current.getNextNode();
            } 
            current.setNextNode(item);
            size++;
            current.getNextNode().setIndex(size - 1);
            System.out.println("Added a new link to the existing linkedlist at " + current.getNextNode().getIndex());   
        }
        return null;
    }

    public SampleableList sample() { 
        if (firstLink == null){
            System.out.println("List is empty, so returning an empty sampableList");
            return emptyList = new SampleableListImpl();
        }
        Object current = firstLink;
        if (firstLink.getNextNode().getNextNode() != null){
            nonEmptyList = new SampleableListImpl();
            System.out.println("Adding to firstNode in nonEmptyList");
            nonEmptyList.add(firstLink);
            while (current.getNextNode().getNextNode() != null){
                current = current.getNextNode().getNextNode();
                System.out.println("Adding " + current.getIndex() + " to nonEmptyList");
                nonEmptyList.add(current);
            }
        } else {
                nonEmptyList.firstLink = current;
                System.out.println("There is only a head - no other objects to sample");
            }
        System.out.println("returning nonEmptyList");
        return nonEmptyList;
    }

}

And I am running this 我正在运行这个

SampleableListImpl sampList = new SampleableListImpl();
        Object ob = new Object();
        Object ob1 = new Object();
        Object ob2 = new Object();
        Object ob3 = new Object();
        Object ob4 = new Object();
        sampList.add(ob);
        sampList.add(ob1);
        sampList.add(ob2);
        sampList.add(ob3);
        sampList.add(ob4);
        sampList.sample(); 

When all 5 objects are added just before call sample() : 当在调用sample()之前添加所有5个对象时:

  • ob.getNextNode() will return ob1 . ob.getNextNode()将返回ob1
  • ob1.getNextNode() will return ob2 . ob1.getNextNode()将返回ob2
  • ob2.getNextNode() will return ob3 . ob2.getNextNode()将返回ob3
  • ob3.getNextNode() will return ob4 . ob3.getNextNode()将返回ob4
  • ob4.getNextNode() will return null . ob4.getNextNode()将返回null

sample() will in the first loop add ob again: sample()将在第一个循环中再次添加ob

  • ob.getNextNode() will return ob1 . ob.getNextNode()将返回ob1
  • ob1.getNextNode() will return ob2 . ob1.getNextNode()将返回ob2
  • ob2.getNextNode() will return ob3 . ob2.getNextNode()将返回ob3
  • ob3.getNextNode() will return ob4 . ob3.getNextNode()将返回ob4
  • ob4.getNextNode() will return ob . ob4.getNextNode()将返回ob

In the second loop within sample() it will try to add ob2 , but it can no longer reach the end of your list. sample()的第二个循环中,它将尝试添加ob2 ,但它不再可以到达列表的末尾。

What you could do to get around this, is create a copy of each object you want to add from sample() (and set their next node to null before adding). 要解决此问题,您需要做的是从sample()创建要添加的每个对象的副本(并在添加之前将其下一个节点设置为null )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM