Buttercola: Airbnb: 2D Iterator with remove()
Airbnb: 2D Iterator with remove()
Implement a 2D iterator, with method next(), hasNext() and remove().
Understand the problem:
The question is close to the Leetcode Flatten 2D iterator. The only thing needs to take care is the remove() method.
According to the definition of the remove(),
Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next(). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.
So the remove() method actually removes the element returned from the next().
Understand the problem:
The question is close to the Leetcode Flatten 2D iterator. The only thing needs to take care is the remove() method.
According to the definition of the remove(),
Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next(). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.
So the remove() method actually removes the element returned from the next().
To safely remove from a collection while iterating over it you should use an Iterator.
Read full article from Buttercola: Airbnb: 2D Iterator with remove()
No comments:
Post a Comment