Difference between revisions of "Iterator"

From CDOT Wiki
Jump to: navigation, search
Line 2: Line 2:
 
The '''Iterator Pattern''' is commonly used in Computer Science as a way to access elements of a collection, regardless of how they were implemented. An iterator may also be called a cursor.
 
The '''Iterator Pattern''' is commonly used in Computer Science as a way to access elements of a collection, regardless of how they were implemented. An iterator may also be called a cursor.
 
== Description ==
 
== Description ==
    An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection. Depending on the language the iterator is implemented in, other functionality may be added to the iterator object.
+
An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection. Depending on the language the iterator is implemented in, other functionality may be added to the iterator object.
  
    The purpose of an iterator is to give a user a way to process each element of a container while separating the user from the internal structure of the container. This allows the container to store elements however it wants to, while allowing the user to use it as if it were just a simple list or sequence. An iterator class is usually designed in coordination with the container class, which usually provides methods or functions for creating iterators.
+
The purpose of an iterator is to give a user a way to process each element of a container while separating the user from the internal structure of the container. This allows the container to store elements however it wants to, while allowing the user to use it as if it were just a simple list or sequence. An iterator class is usually designed in coordination with the container class, which usually provides methods or functions for creating iterators.
  
    An easy way to think of iterators, is to look at Linked Lists, because they operate very similarly.  
+
An easy way to think of iterators, is to look at Linked Lists, because they operate very similarly.  
  
 
=== Implicit Iterators ===
 
=== Implicit Iterators ===
    Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object.
+
Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object.

Revision as of 21:46, 20 January 2007

The Iterator Pattern is commonly used in Computer Science as a way to access elements of a collection, regardless of how they were implemented. An iterator may also be called a cursor.

Description

An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection. Depending on the language the iterator is implemented in, other functionality may be added to the iterator object.

The purpose of an iterator is to give a user a way to process each element of a container while separating the user from the internal structure of the container. This allows the container to store elements however it wants to, while allowing the user to use it as if it were just a simple list or sequence. An iterator class is usually designed in coordination with the container class, which usually provides methods or functions for creating iterators.

An easy way to think of iterators, is to look at Linked Lists, because they operate very similarly.

Implicit Iterators

Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object.