Encyclopedia > Iterator

  Article Content

Iterator

In computer programming, an iterator is an object that maintains a type of cursor used for processing each element in a list or set of similiar items.

Iterators are are frequently used in languages like Java; they are crucial in gaming languages such as UnrealScript.

An example usage of an iterator (for java) can be explained here.

Iterator it = list.iterator();
while(it.hasNext()){
   Object spot = it.next();
}

The above example implies that the Object (called list) supports an iterator method. This hypothetical iterator then supplies the methods hasNext and next that are used for scrolling through the list of items.

The variable spot is a type of cursor marking the current element.

foreach $spot (@list){
    print "$spot\n";
}

The above example (in perl) shows the same logic, but is not object oriented. Here @list is an Array of items. The iterator is the foreach keyword, and is used to traverse the list.

Each element in the list is represented by the variable $spot the code within the block can then operate on that particular element.

Physically, an iterator can be viewed as a ruler or (other marker, such as a human finger) placed over a paper list. Each entry is "pointed at" by the ruler, to get to the next entry, one would slide the ruler down one line. This implements a type of physical iterator.

See Also: Iterator pattern



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Quadratic formula

... are two different solutions x, both of which are complex numbers. The two solutions are complex conjugates of each other. (In this case, the parabola does not intersect the ...

 
 
 
This page was created in 33.7 ms