This article needs additional citations for verification. (June 2010) |
In computer programming, an iterator is an object that progressively provides access to each item of a collection, in order.[1][2][3]
A collection may provide multiple iterators via its interface that provide items in different orders, such as forwards and backwards.
An iterator is often implemented in terms of the structure underlying a collection implementation and is often tightly coupled to the collection to enable the operational semantics of the iterator.
An iterator is behaviorally similar to a database cursor.
Iterators date to the CLU programming language in 1974.
A user-defined iterator usually takes the form of a code reference that, when executed, calculates the next item in a list and returns it. When the iterator reaches the end of the list, it returns an agreed-upon value.
Iterators were introduced as constructs to allow looping over abstract data structures without revealing their internal representation.
You can think of an iterator as pointing to an item that is part of a larger container of items.