Java自定义迭代器无限期地停留在foreach循环中

时间:2014-03-03 01:43:03

标签: java iterator singly-linked-list

我正在研究一个单链表迭代器,在调试过程中它不会被传递给for循环而且我不知道为什么。

这是我的Iterator和Node:

class Node<E> {
    E data;
    Node<E> next;
    public Node(E obj){
        data = obj;
        next = null;
    } //end class node
}

private Node<E> head,tail;
int currentSize = 0;//initializes the size to 0   

class IteratorHelper implements Iterator<E>{
    private Node<E> iteratorptr;

    public IteratorHelper(){
        this.iteratorptr = head;
    }
    public boolean hasNext(){
        return iteratorptr != null && iteratorptr.next != null;
    }
    public E next(){
        if(!hasNext())
            return null;
        iteratorptr = iteratorptr.next;
        return iteratorptr.data;
    }
    public void remove(){
        throw new UnsupportedOperationException();
    }
}

为了测试迭代器和我的链表实现,我的导师给了我这个:

// check the list with the iterator. If n = 25, this should print
// 25 24 23 22 21 ... 5 4 3 2 1
System.out.println("Using the iterator");
for (Integer i : llist)
    System.out.print(i + " ");
System.out.println();

然后打印出所需的25 ... 1结果,但是当我的迭代器遇到问题时,我清空列表并添加1个项目:

// now add one thing to the list
llist.addLast(n+1);

// this should be the only thing in the list
for (int i : llist)
if (i != (n+1))
    System.err.println("There should be only one thing in the list, but we got " + i);

在调试过程中,它会无限期地陷入for循环,i = 26并且永远不会改变。 我试图修改我的迭代器但没有取得任何成功。

这是我在Stack上的第一篇文章,对于任何糟糕的帖子做法我很抱歉!谢谢你的时间!

编辑:

这是我的节点类:在另一个作业中,我已经确认它使用不同的测试文件。

class Node<E> {
    E data;
    Node<E> next;
    public Node(E obj){
        data = obj;
        next = null;
    } //end class node
}

这些是我的删除方法:

public E removeFirst() {
    if (head == null)
        return null;
    E tmp = head.data;
    if (head == tail)
        head = tail = null;
    else {
        head = head.next;
    }
    currentSize--;
    return tmp;
}

public E removeLast() {
    Node<E> previous = head;
    if (head == null)
        return null;
    E temp = tail.data;
    if (head.next == null)
        head = tail = null;
    else {
        while (previous.next != tail) {
            previous = previous.next;
        }
        tail = previous;
        tail.next = null;
    }
    currentSize--;
    return temp;
}

以下是addLast的第二次编辑:

         public void addLast(E obj){
    Node<E> newNode =  new Node<E>(obj);
    if(head == null) head = tail = newNode;
    if(head.next==null){
        head.next = tail.next = newNode;
        tail = newNode;}
    else{
        tail.next = newNode;
        tail = newNode;}
        currentSize++;}

1 个答案:

答案 0 :(得分:0)

清空列表后,headtail为空,请阅读以下评论。

public void addLast(E obj){
Node<E> newNode =  new Node<E>(obj);
if(head == null) head = tail = newNode; /*You set the head and tail here*/
if(head.next==null){ /*This is obviously null because you only have one node and based on your node class*/
head.next = tail.next = newNode; /*Yet you add this to your next, giving you an infinite loop, a node whose next is itself*/
tail = newNode;}

您可以将此作为addLast方法

public void addLast(E obj)
{
    Node<E> newNode = new Node<E>(obj);
    if(head == null) 
    {
        head = tail = newNode;
    }   
    else
    {
        tail.next = newNode;
        tail = tail.next;
    }
    currentSize++;
}