DEV Community

JB
JB

Posted on • Updated on

Extending An Iterator

Resources:

  1. How to design a skip iterator
  2. Java's Iterator reference
  3. C#'s IEnumerator reference
  4. Wikipedia article on iterators

Takeaways:

  • Iterators allow us to traverse and access elements in collections without the use of indexing.
  • Iterators expose a consistent way to traverse all types of data structures, making the code more resilient to change.
  • Iterators can allow the underlying collection to be modified during traversal, unlike when using indexing to iterate over a collection (like using a for loop on an array). This can mean insertion/deletion of items during traversal.
  • Iterators exist in many languages, in C# they are called enumerators.
  • Time & Space complexity in the code comments

Below you will find a class that takes an enumerator in it's constructor and extends the basic functionality of an iterator with the addition of Skip(), Peek(), & Remove() operations:

As always, if you found any errors in this post please let me know!

Latest comments (0)