为什么我要进入循环?

时间:2017-01-07 18:43:23

标签: java loops

它工作正常并给我正确的输出,直到它输入最后一个要添加到nonEmptyList方法sample()的对象。我已经设法找出它在循环中的位置,这是add(item)方法中的while循环。我无法改变方法或返回,所以如果有人能提出一种方法我可以阻止这种无限循环,那将是值得赞赏的。

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;
    }

}

我正在运行

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(); 

1 个答案:

答案 0 :(得分:0)

在调用sample()之前添加所有5个对象时:

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

sample()将在第一个循环中再次添加ob

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

sample()中的第二个循环中,它会尝试添加ob2,但它无法再到达列表的末尾。

你可以做些什么来解决这个问题,就是在sample()创建要添加的每个对象的副本(并在添加之前将其下一个节点设置为null)。

相关问题