Copy 2 ArrayCollections

This is actually written by my Colleague – Ajit Parthan. Have modified it a little bit so that it returns an ArrayCollection. This method takes in two ArrayCollections and returns a single ArrayCollection which is a sum of the first two. Ex –

ArrayCollection SRC 1 = A, B, C, D
ArrayCollection SRC 2 = 1, 2, 3, 4

ArrayCollection SUM = A, B, C, D, 1, 2, 3, 4

I hope this makes it clear enough πŸ™‚ . If there are more doubts, try it out πŸ˜‰ .

private function copyArrayCollection(src1:ArrayCollection,
var dest:ArrayCollection = new ArrayCollection();
if(src1 != null && src1.length > 0){
for(var i:int = 0; i<src1.length; i++){
dest = src2;
return dest;

Am updating this post based on a comment posted by Abdul. There seems to be a better way to achieving this by –

public function copyArrayCollection (src1:ArrayCollection, src2:ArrayCollection):ArrayCollection
var result:ArrayCollection = new ArrayCollection(src1.source.concat(src2.source));
return result;


21 Responses to Copy 2 ArrayCollections

  1. Abdul Qabiz says:

    public function copyArrayCollection (src1:ArrayCollection, src2:ArrayCollection):ArrayCollection
    var result:ArrayCollection = new ArrayCollection (src1.source.concat (src2.source));
    return result;

    It doesn’t do the deep-copy, you can achieve that by using mx.utils.ObjectUtil.copy (..)


  2. concat () method

    AS3 function concat(… args):Array
    Concatenates the elements specified in the parameters with the elements in an array and creates a new array. If the parameters specify an array, the elements of that array are concatenated.

    Maybe you can make use of concat??

  3. Sorry Abdul Qabiz, didn’t read your posting carefull enough! Keep it Real!

  4. udayms says:

    Maybe I should update the main post with the correction. Lets not have readers doing the wrong thing because they didnt read the comments πŸ™‚

  5. Sascha Rahlff says:

    hi guys, i found a simple way to make a deep copy!

    // This is the Source AC
    var ac:ArrayCollection = new ArrayCollection([{text:”test 1″}, {text:”test 2″}, {text:”test 3″}]);

    // Copy by Reference
    var copyAc:ArrayCollection = ac;

    // Deep Copy
    var deepCopy:ArrayCollection = new ArrayCollection(ac.source.slice(0, ac.length));

    • ups101 says:

      good stuff Sascha Rahlff, but i think what you call a deep copy is really a shallow copy. within slice area, arrays ac and deepCopy contains references to the same objects.

      In my case, this is just what i needed, so thanks πŸ™‚

  6. Alejandro Przybilla says:

    mx.utils.ObjectUtil.copy (..) So useful.
    =) thank you for this post,It actually saved me a lot of time.

    • Landiwe says:

      It’s a custom child theme based on the iThemes Builder frwoeamrk and the Builder child Adept. With one exception, I have never used a theme created by someone else on a site I’ve developed for either me or a client. I really like Builder; it’s flexible, it’s powerful, it plays well with a large number of plugins, there are a lot of child themes available right out of the box, and I can dig into the code and adapt a child theme to do almost anything.If you’re interested in talking about your web strategy, let’s talk! Go here and set up a free introductory call.

  7. CandyShopGirl says:


    What do you think about Apple Iogo? >:)

  8. mambotx says:

    Tried to get this to work but to no avail.

    However, I did get this to work!

    a1 = new ArrayCollection();
    for each (var obj:Object in a2) {

    Somehow the ObjectUtil.copy breaks the connection.

  9. pyxb hzpjc oyida fhynaxkqc gzpiuyrdn aikjoq dvbat

  10. Sanjeev says:

    Thanks for ObjectUtil.copy

  11. Saravanan says:

    mambotx thanks a lot!!!!

  12. Sony says:

    Thank you Sascha Rahlff !!

    Your trick with array slice DID WORK.. πŸ™‚

    You saved my day.. πŸ™‚

  13. Pingback: ArrayCollection clonado, duplicado, copiado, etc » Bruno bg + ADOBE FLEX

  14. B says:

    Isn’t that a soft copy? You cannot manipulate the second array collection without affecting the first, as they both refer to objects at the same memory collection.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: