package com.ericfeminella.collections
{
import flash.errors.IllegalOperationError;
/**
*
* Concrete Iterator implementation which provides an API for
* iterating over an <code>Array</code>.
*
* @example A basic example is as follows:
*
* <listing version="3.0" >
*
* var array:Array = new Array( "A", "B", "C" );
* var it:Iterator = new ArrayIterator( array );
*
* while ( it.hasNext() )
* {
* trace( it.next(), it.position());
* }
*
* //outputs:
* //A, 0
* //B, 1
* //C, 2
*
* </listing>
*
* @see com.ericfeminella.collections.Iterator
*
*/
public class ArrayIterator implements Iterator
{
/**
*
* <code>Array</code> in which the <code>ArrayIterator</code>
* is to traverse.
*
*/
private var items:Array;
/**
*
* Defines the index in which the <code>ArrayIterator</code>
* is to traverse.
*
*/
private var index:int = 0;
/**
*
* Instantiates a new <code>ArrayIterator</code> instance
*
* @param the Array in which to iterate over
*
*/
public function ArrayIterator(array:Array = null)
{
items = array;
}
/**
*
* Determines if elements remain in the <code>Array</code>.
*
* @inheritDoc
*
*/
public function hasNext() : Boolean
{
return index < items.length;
}
/**
*
* Returns the next item element in the <code>Array</code>.
*
* @return next array element based on the current index
*
*/
public function next() : *
{
return items[index++];
}
/**
*
* Resets the index of the <code>ArrayIterator</code>
* instance to 0.
*
*/
public function reset() : void
{
index = 0;
}
/**
*
* Determines the indexed position of the <code>Array</code>.
*
*/
public function position() : int
{
return index;
}
}
}