Java自定义迭代器不确定地卡在每个循环中

Java自定义迭代器不确定地卡在每个循环中,java,iterator,singly-linked-list,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 currentSiz

我正在开发一个单链表迭代器,在调试过程中,它不会通过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();
    }
}
这将打印出所需的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
}
类节点{
E数据;
节点下一步;
公共节点(E obj){
数据=obj;
next=null;
}//结束类节点
}
以下是我的删除方法:

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;
}
public E removeFirst(){
if(head==null)
返回null;
E tmp=头数据;
如果(头=尾)
头=尾=空;
否则{
head=head.next;
}
当前大小--;
返回tmp;
}
公共E removeLast(){
节点前一个=头;
if(head==null)
返回null;
E温度=尾部数据;
if(head.next==null)
头=尾=空;
否则{
while(previous.next!=tail){
上一个=上一个。下一个;
}
尾=前一个;
tail.next=null;
}
当前大小--;
返回温度;
}
以下是使用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++;}
public void addLast(E obj){
Node newNode=新节点(obj);
如果(head==null)head=tail=newNode;
if(head.next==null){
head.next=tail.next=newNode;
tail=newNode;}
否则{
tail.next=newNode;
tail=newNode;}
currentSize++;}

清空列表后,您的
头部
尾部
为空,请阅读下面的注释

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
方法吗?我想你没有回答卡卡洛特的问题,你如何从列表中清除元素?“在我清空列表并向其中添加1项后”,如何清空它?你会先打n次电话吗?删除最后n次?还是你用另一种方法?非常感谢这完全修复了它!
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;}
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++;
}