2011-09-18 26 views
0

我想創建自定義迭代器元素。課程本身在下面。as3:如何創建自定義迭代器

public class ArrayBasedIterator implements IteratorInterface 
{ 
    // Holds Array of elements which are iterated 
    private var _container:Array; 

    // Holds current index 
    private var _index:uint = 0; 

    public function ArrayBasedIterator(data:Array) 
    { 
     if (!data) 
       throw new Error("Cannot create iterator for null data"); 
     _container = data; 
    } 

    /** 

    * Returns next node if it exists, null otherwise*/ 
    public function next():TreeNode 
    { 
     if (_index > _container.length) 
     { 
      return null; 
     } 
     else { 
      _index += 1; 
     } 

     //TODO: implement function 
     return _container[_index]; 
    } 

    /** 

    * Returns prev node if it exists, null otherwise*/ 

    public function prev():TreeNode 
    { 
     if (_index < 0) 
     { 
      return null 
     } 
     else { 
      _index -= 1; 
     } 

     //TODO: implement function 
     return _container[_index]; 
    } 

    public function len():int 
    { 
     return _container.length; 
    } 

} 

問題是。如果我對某些元素有這樣的迭代器,是否有可能用於......的結構?我需要更改以使用它? (除了使用代替當然迭代器只是陣列)

+0

可能重複「爲每個」在ActionScript?](http://stackoverflow.com/questions/3787289/playing-nicely-with-for-each-in-actionscript) –

回答