3

I want to iterate over an ArrayCollection in Flex while there can be items added and removed.

Since i didn't find a way to use a "classic" Iterator like in Java, which would do the job. I tried the Cursor. But it doesn't really work the way i want it to be ;) So how do I do it nicely ?


    var cursor:IViewCursor = workingStack.createCursor();

    while (!cursor.afterLast)
    {
        // Search
                    deepFirstSearchModified(cursor.current.node,nodeB,cursor.current.way);
        // Delete Node
        cursor.remove();
        // Next Node
        cursor.moveNext();

    }
Dukeatcoding
  • 1,363
  • 2
  • 20
  • 34

5 Answers5

3

I think better to use New Collection/Array for opertations as

private function parseSelectedItem(value:IListViewCollection):IListViewCollection{
 var result:Array = new Array();
    for each(var item:Object in value)
    {
        //result.push();
        //result.pop();
    }
    return new ArrayCollection(result) ;
}

Hopes that helps

Imran
  • 2,906
  • 1
  • 19
  • 20
2

Try to use the following:

for (var i:int = myArrayCollection.length - 1; i >= 0; i--) {
   myArrayCollection.removeItemAt(i);
}
Constantiner
  • 14,231
  • 4
  • 27
  • 34
1

There is a solution for your problem:

http://www.ericfeminella.com/blog/actionscript-3-apis/

Have a look at the CollectionIterator class.

Cheers

Dennis Jaamann
  • 3,547
  • 2
  • 23
  • 42
0

Take a look at ActionLinq. It implements the .Net Linq2Objects pattern, including IEnumerable. Of course, you need to be careful, because you are modifying the items you are iterating over...

var workingStack:ArrayCollection = getData();
var iterator:IEnumerable = Enumerable.from(workingStack);

for each(var item:String in iterator) {
  doSomethingTo(workingStack);
}
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
-1

In flex (or actionscript) any change that you do, is visible instantly. So you can do what you want in a for:

    for (var i : Number = myArrayCollection.length; i > 0; i--) {
       myArrayCollection.removeItemAt(i - 1);
    }

I think that should work fine.

artaxerxe
  • 6,281
  • 21
  • 68
  • 106
  • ok that would be nice, because in Java it would throw an exception. have to try it – Dukeatcoding May 24 '11 at 08:44
  • removeElement is not a method of an arrayCollection – Dennis Jaamann May 24 '11 at 08:46
  • @Dukeatcoding, it will indeed throw an exception due to the fact that you will try to delete elements that are not longer at a certain index – Dennis Jaamann May 24 '11 at 08:47
  • @Duke: working with flex you will see that it doesn't have concurrency approach for developer. You won't have multithreading problems in flex! – artaxerxe May 24 '11 at 08:49
  • you have to use removeItemAt instead of removeElement. i will try out the for each in a few minutes, just have to finish something – Dukeatcoding May 24 '11 at 08:53
  • @Dennis: You're right. @Duke : now I think it should work. If it doesn't, you should try in same manner, just changing the myArrayCollection.length to your variable – artaxerxe May 24 '11 at 08:53
  • @All, the reason why it doesn't work this way is described here: http://form-function.blogspot.com/2008/03/removing-items-from-array-collection.html – Dennis Jaamann May 24 '11 at 08:56
  • 2
    You have to iterate backwards or you end up skipping elements. If you remove the current element, then increase the counter, you end up skipping the next element – Laplie Anderson Oct 25 '11 at 15:23