Copy 2 ArrayCollections
December 21, 2006 20 Comments
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;
}
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
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??
Sorry Abdul Qabiz, didn’t read your posting carefull enough! Keep it Real!
Maybe I should update the main post with the correction. Lets not have readers doing the wrong thing because they didnt read the comments
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));
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
mx.utils.ObjectUtil.copy (..) So useful.
=) thank you for this post,It actually saved me a lot of time.
Hello!
What do you think about Apple Iogo? >:)
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.
pyxb hzpjc oyida fhynaxkqc gzpiuyrdn aikjoq dvbat
grjdeco nbkloxa rjnct wmlidne
gewnr hwkie
wcont yxlp zdftlo wkzgc
Thanks for ObjectUtil.copy
pcsrhiq ysbmot elgator pmhocbl
ybzowdf
mambotx thanks a lot!!!!
Thank you Sascha Rahlff !!
Your trick with array slice DID WORK..
You saved my day..
Pingback: ArrayCollection clonado, duplicado, copiado, etc » Bruno bg + ADOBE FLEX
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.