var a:Array = ["a","b","c"];
var b:Array;
/* insert code here to copy 'a' and assign it to 'b'*/
Taken from the As3 reference guide:
The Array class has no built-in method for making copies of arrays. You can create a shallow copy of an array by calling either the concat() or slice() methods with no arguments. In a shallow copy, if the original array has elements that are objects, only the references to the objects are copied rather than the objects themselves. The copy points to the same objects as the original does. Any changes made to the objects are reflected in both arrays.
Concat would be the way to go if you choose between concat and slice since concat is faster in terms of performance.
Read more about the subject here: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee7.html
To clarify:
private function shallowCopy():void{
var a:Array = ["h", "e", "l", "l", "o"];
var b:Array = a.concat();
trace("Shallow copy:");
trace("Before delete: " + a);
trace("Before delete: " + b);
delete a[0];
trace("After delete: " + a);
trace("After delete: " + b);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With