繁体   English   中英

在Java中实现remove()迭代器方法

[英]Implementing remove() Iterator Method in Java

因此,我正在使用通用的LinkedList,并且需要能够使用迭代器删除它的后半部分。 但是,我似乎无法正常工作。 这是迭代器调用:

    Iterator<String> itr = seq.iterator();
    for (int i = 0; i < N / 2; i++)
    {
        itr.next();
    }

    for (int i = 0; i < N / 2; i++)
    {
        itr.next();
        itr.remove();
    }

这是我的迭代器方法:

    boolean canRemove = false;
    int previousLoc = -1;
    Node<T> current = head;

    @Override
    public boolean hasNext()
    {
        return current != null;
    }

    @Override
    public T next()
    {
        if (hasNext())
        {
            T data = current.getData();
            current = current.getLink();
            previousLoc++;
            canRemove = true;
            return data;
        }
        throw new NoSuchElementException();
    }

    public void remove()
    {
        if (!canRemove)
        {
            throw new IllegalStateException();
        }

        SortedLinkedList.this.remove(previousLoc);
        canRemove = false;

    }

它在第二个for循环下的itr.next()调用上给出NoSuchElementException。 我最好的猜测是,这可能与我确定上一个节点的位置有关。 该类具有getPrevious()方法:

private Node<T> getPrevious(T entry)
{
    Node<T> previous = null;
    Node<T> traverse = head;
    while (traverse != null)
    {
        //if(entry.compareTo((T) traverse.getData()) > 0)
        if (traverse.getData().compareTo(entry) < 0)
        {
            previous = traverse;
            traverse = traverse.getLink();
        }
        else
        {
            return previous;
        }
    }
    return previous;
}

还有一个getPosition方法:

public int getPosition(T anEntry)
{
    Node<T> traverse = head;
    for (int i = 0; i < manyNodes; i++, traverse = traverse.getLink())
    {
        if(anEntry.compareTo(traverse.getData()) == 0)
        {
            return i;
        }
    }
    throw new IllegalArgumentException("Element not in list");
}

但是,如果我尝试类似

SortedLinkedList.this.remove(getPosition((T) getPrevious((T) current)));

我得到“ solution.Node无法转换为java.lang.Comparable”

即使类头确实扩展了它:

public class SortedLinkedList<T extends Comparable<? super T>> implements Iterable<T>

编辑:这是删除方法:

public T remove(int givenPosition)
{
    T dataToReturn;

    if (givenPosition < 0 || givenPosition >= manyNodes)
    {
        return null;
    }
    if (givenPosition == 0)
    {
        dataToReturn = head.getData();
        head = head.getLink();
    }
    else
    {
        Node<T> previous = head;
        for (int i = 0; i < givenPosition - 1; i++)
        {
            previous = previous.getLink();
        }
        Node<T> oneToDelete = previous.getLink();
        dataToReturn = oneToDelete.getData();
        previous.setLink(oneToDelete.getLink());
        oneToDelete.setLink(null);
    }
    manyNodes--;
    return dataToReturn;
}

尝试这样的事情:

public void remove()
{
    if (!canRemove)
    {
        throw new IllegalStateException();
    }
    SortedLinkedList.this.remove(previousLoc--);
    canRemove = false;
}

就像在循环中,对next()的调用增加了previousLocremove()方法从列表中删除了该项,它并没有更改previousLoc的值。 因此, previousLoc保持递增,而所有元素都将被删除。

为了在工作中看到这一点,您应该在next()remove()期间打印出previousLoc

暂无
暂无

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

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