Hello, there is a task

private class IteratorImpl implements Iterator<Object> { public boolean hasNext() { //returns true if the iteration has more elements // ... } public Object next() { // returns the next element in the iteration // ... } public void remove() { //removes from the underlying collection the last element returned by this iterator // ... } } 

The algorithm in the remove method may be as follows:

IF BEFORE CALLING, REMOVE WAS NOT CALLED METHOD next OR BEFORE CALLING REMOVAL WAS CALLED remove (call again remove) TO CLEAR EXCLUSION (just insert it into the code): throw new IllegalStateException (); IN THIS PLACE, DETERMINE AND REMOVE THE RELEVANT ELEMENT

implemented the first two methods, stayed remove.

Question: I don’t understand how to implement it? How to find out which methods were called before remove, and I don’t understand how to remove either.

  • 2
    You can do a boolean, in the next call you can do it, and in the remove call you can do it false. In remove check if it is false to throw an exception (at the beginning of the method !!!) - user31238
  • Thank you very much, after your comment the brains turned on and did everything. - Seryoga

0