Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a shallow copy of an Array in ActionScript 3

var a:Array = ["a","b","c"];

var b:Array;

/* insert code here to copy 'a' and assign it to 'b'*/
like image 931
Gareth Davis Avatar asked Dec 02 '22 02:12

Gareth Davis


1 Answers

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);            
    }
like image 91
rzetterberg Avatar answered Dec 09 '22 10:12

rzetterberg