LEARNING JOURNAL

I created this learning journal to practice writting and to help me learn by writting everything down. Source code here

1/27/2024

Peeking Iterator #2nd attempt

This is my second attempt at solving the Peeking Iterator problem.

Let me explain my train of thought.

All iterators have two methods: next and hasNext. With this iterator, we need another one: peek.

What peek does is to get the current (or next) item without advancing the iterator to the next item. To do that, we need to store the next item in a private variable, and then each time user calls peek, we return the value of that variable.

In the next function, we not only return the current value but also advance the iterator to the next value. We do the "advancing" by calling the private iterator's next function, but store it to the private value.