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,
src2:ArrayCollection):ArrayCollection{
var dest:ArrayCollection = new ArrayCollection();
if(src1 != null && src1.length > 0){
for(var i:int = 0; i<src1.length; i++){
src2.addItem(src1.getItemAt(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;
}

About these ads

20 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 (..)

    -abdul

  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.

  7. CandyShopGirl says:

    Hello!

    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) {
    a2.addItem(ObjectUtil.copy(obj));
    }

    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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: