简体   繁体   中英

Implementing Iterator.remove()

I have implemented a generic CircularArrayRing and I am working on implementing an iterator for it. I wonder how is the remove() method of an iterator work. 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:

you have the option to not implement the method in which case you simply throw a 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:

  • the next() method on the iterator has not been called yet
  • the remove() method is called more than once for the same selected element (which has been removed after the first call)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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