piątek, 17 lutego 2012

How to copy ArrayCollection in Flex

Way to make a copy of ArrayCollection is very simple, we just need to create a new instance of ArrayCollection and pass array to constructor:


var ac1:ArrayCollection = new ArrayCollection(["a", "b", "c", "d"]);
trace("ac1: " + ac1);

var ac2:ArrayCollection = new ArrayCollection(ac1.toArray());
trace("ac2: " + ac2);

ac2.setItemAt("X", 2);
               
trace("ac1: " + ac1);
trace("ac2: " + ac2);

The result of this code will be:

ac1: a,b,c,d
ac2: a,b,c,d
ac1: a,b,c,d
ac2: a,b,X,d

So as You can see, ac1 wasn't modified. I also tried this with class objects, insted of simple string or number.

Brak komentarzy:

Prześlij komentarz