简体   繁体   English

实现Iterator.remove()

[英]Implementing Iterator.remove()

I have implemented a generic CircularArrayRing and I am working on implementing an iterator for it. 我已经实现了通用的CircularArrayRing,并且正在为其实现迭代器。 I wonder how is the remove() method of an iterator work. 我想知道迭代器的remove()方法如何工作。 Does it have to actually modify the ring or return a copy of it with the specified element been removed? 它是否必须实际修改环或返回已删除指定元素的环的副本?

import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class CircularArrayRing<E> extends AbstractCollection<E> implements Ring<E>
{

    private int elements;
    private int front;
    private E[] ring;

@SuppressWarnings("unchecked")
public CircularArrayRing()
{
    ring = (E[]) new Object[10];
    front = 0;
}

@SuppressWarnings("unchecked")
public CircularArrayRing(int size)
{
    ring = (E[]) new Object[size];
    front = 0;
}

@Override
public boolean add(E e) 
{

    ring[front] = e;
    front++;

    if(front == ring.length)
    {
        front = 0;
    }

    if(elements < ring.length)
    {
        elements++;
    }

    return false;
}

@Override
public Iterator<E> iterator() 
{   
    return new RingIterator();
}

@Override
public int size() 
{
    return elements;
}

//returns the element at the specified position
//the last added element has index 0, the second most recent has index 1 etc
@Override
public E get(int index) throws IndexOutOfBoundsException 
{
    if(index > elements - 1 || index > ring.length - 1) 
    {   
        throw new IndexOutOfBoundsException();
    }
    else
    {   
        if (index < front) 
        {
            return ring[front - 1 - index];
        }
        else 
        {
            return ring[ring.length + front - 1 - index];
        }
    }


  }


@SuppressWarnings("rawtypes")
private class RingIterator implements Iterator<E>
{
    private int currentIndex = 0;


    // returns the next element in the iterator
    @SuppressWarnings("unchecked")
    @Override
    public E next() 
    {
        if(currentIndex > ring.length - 1)
        {
            throw new NoSuchElementException();
        }


        if (currentIndex < front)
        {   
            return (E) ring[front - 1 - currentIndex++];
        }
        else
        {
            return (E) ring[ring.length + front - 1 - currentIndex++];


        }

    }   


    //checks if there is another element 
    @Override
    public boolean hasNext() 
    {
        if(currentIndex < elements )
        {
            return true;
        }
        else
        {   
            return false;
        }

    }



    public void remove()
    {


    }

}   

}

The Javadoc is clear about this: Javadoc对此很清楚:

you have the option to not implement the method in which case you simply throw a UnsupportedOperationException 您可以选择不实现该方法,在这种情况下,您只需抛出UnsupportedOperationException

If you implement it, you must remove the element that is currently selected by the iterator from the underlying collection. 如果实现它,则必须从基础集合中删除迭代器当前选择的元素。 In the following cases where there is no element selected, you throw an IllegalStateException: 在以下没有选择任何元素的情况下,将引发IllegalStateException:

  • the next() method on the iterator has not been called yet 迭代器上的next()方法尚未被调用
  • the remove() method is called more than once for the same selected element (which has been removed after the first call) 对于相同的选定元素(在第一次调用后已将其删除),多次调用了remove()方法。

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

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