I was looking for an Iterator implementation in ActionScript 3 to avoid reinventing the wheel for the umpteenth time but couldn't find one on Google, so I whipped one up quick that you can download here. This should save someone a full 3 minutes of work in the future....
Usage is pretty simple and straightforward. The download includes the Iterator interface as well as a simple implementation of ArrayIterator for iterating over an array of elements:
import com.darronschall.util.*;
var arr:Array = [1, 2, 3];
var it:Iterator = new ArrayIterator( arr );
while ( it.hasNext() )
{
trace( it.next() ); // 1, 2, 3
}
var it2:Iterator = new ArrayIterator( null );
trace( it2.hasNext() ); // false
You can use this code without restriction under the MIT License.
Tags: ActionScript, ActionScript 3, Design Patterns, Iterator
Hello Darron,
thanks for sharing,
I'm using existing api but have been looking for different imlementation to have less dependancy on flex sdk:
sample based on existing sdk:
var arr:Array = [1, 2, 3]
var ic:IViewCursor = new ArrayCollection(arr).createCursor();
while(!ic.afterLast){
trace(ic.current);
ic.moveNext()
};
arr = [];
ic = new ArrayCollection(arr).createCursor();
trace(ic.moveNext());
regards,
Peter Blazejewicz