FLEX

[FLEX] ArrayCollection Copy

단순대왕 2008. 10. 23. 17:26

The following code will not create a copy but instead will create a reference to the same ArrayCollection.

var a1:ArrayCollection = new ArrayCollection();
a1.addItem(new Object());
a1.addItem(new Object());
var a2:ArrayCollection = new ArrayCollection();
a2 = a1; // a2 and a1 will refer to the same ArrayCollection object

ArrayCollection does not have a copy, concat, join or splice methods which return a new array so we need to do the copy manually.

var a1:ArrayCollection = new ArrayCollection();
a1.addItem(new Object());
a1.addItem(new Object());
var a2:ArrayCollection = new ArrayCollection();
for (var i:uint = 0; i < a1.length; i++) {
    a2.addItem(a1.getItemAt(i));
}

Note that a2 will still contain references to the same objects that a1 has. This is useful if you want to create a shuffled ArrayCollection but do not want to modify the original, so we create a copy.