View comments | RSS feed

Cloning arrays

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.

In a deep copy, any objects found in the original array are also copied so that the new array does not point to the same objects as does the original array. Deep copying requires more than one line of code, which usually calls for the creation of a function. Such a function could be created as a global utility function or as a method of an Array subclass.

The following example defines a function named clone() that does deep copying. The algorithm is borrowed from a common Java programming technique. The function creates a deep copy by serializing the array into an instance of the ByteArray class, and then reading the array back into a new array. This function accepts an object so that it can be used with both indexed arrays and associative arrays, as shown in the following code:

import flash.utils.ByteArray;

function clone(source:Object):*
{
    var myBA:ByteArray = new ByteArray();
    myBA.writeObject(source);
    myBA.position = 0;
    return(myBA.readObject());
}

Flash CS3


Comments


Stick1337 said on Mar 25, 2008 at 2:01 PM :
I know slightly less than nothing about the ByteArray class, but the example given in the documentation for cloning an array doesn't appear to work. I'm getting an error that looks a lot like "Type Coercion failed: cannot convert Object@154003f9 to flash.display.DisplayObject."

Any thoughts on why? The array I'm trying to clone contains instances of a class that extends the Sprite class...Thanks!
swartz1999 said on Mar 27, 2008 at 6:29 PM :
ByteArray readObject() does not work with display objects and other objects that are defined by Flash Player. Complex objects that are defined in ActionScript (from simple types) work.
No screen name said on Apr 8, 2008 at 3:33 PM :
I've been trying to make an example that displays the difference between a shallow and deep copy. So far I haven't been able to find any difference between using the slice/concat methods and the clone function provided in this section. Can someone take a look at my code and tell me what I'm doing wrong?

//-- CLONING ARRAYS --\\
trace("**Cloning Arrays **");
var proto:Object = true;
var original:Array = new Array();
original = [proto];
var shallow:Array = original.slice();
var deep:Array = clone(original);

trace("\tDeep copy = " + deep); // output: true
trace("\tShallow copy = " + shallow); // output: true

trace("\n***VALUE CHANGED***");
proto = false; // object value is changed

trace("\tDeep copy = " + deep); // output: true
trace("\tShallow copy = " + shallow); // output: true <-- should be false ??

// Clone function
import flash.utils.ByteArray;

function clone(source:Object):* // function for deep copy
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}

maybe I'm just confused as to what deep and shallow copies are any explanations would be great thanks.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000092.html