Java自定义迭代器无限期地陷入foreach循环

标签 java iterator singly-linked-list

我正在开发一个单链表迭代器,在调试过程中它不会通过 for 循环,我不明白为什么。

这是我的迭代器和节点:

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

最佳答案

清空列表后,您的 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++;
}

关于Java自定义迭代器无限期地陷入foreach循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22137316/

相关文章:

java - DateTimeFormatter 是否比 SimpleDateFormat 更严格?以毫秒为单位解析日期

java - 在 Spring Boot 应用程序上使用 Flyway 时如何在 H2 中加载初始数据?

Java 验证证书是否与 key 相关联

c++ - 查找 vector 中最接近的值

java - Hibernate 异常 - 无法找到命名参数 [:laboratoryId]

C++ 迭代器值到变量

java - 迭代器 hasNext 意味着列表永远不会为空

java - 在记分板代码中使用什么单链表或数组?

c - 中间输入一个节点,形成一个排序链表

无法从单向链表中删除