8

I find it unfamiliar to work with ActionScript's array assignment by reference methodology. I understand what it's doing, but I somehow find it confusing to manage many arrays with this methodology. Is there a simple way to work with ActionScript arrays where the array assignment is by VALUE rather than REFERENCE? For example, if I want to assign oneArray to twoArray without linking the two arrays to each other forever in the future, how to do it? Would this work?

var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array(3);
for (ii=0; ii<3; ii++) { twoArray[ii] = oneArray[ii]; }

The intent is to be able to change twoArray without seeing a change in oneArray.

Any advice how to assign arrays by VALUE instead of REFERENCE?

---- for reference ----

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

Array assignment is by reference rather than by value. When you assign one array variable to another array variable, both refer to the same array:

var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray; // Both array variables refer to the same array.
twoArray[0] = "z";             
trace(oneArray);               // Output: z,b,c.
taskinoor
  • 45,586
  • 12
  • 116
  • 142
ggkmath
  • 4,188
  • 23
  • 72
  • 129

4 Answers4

11

Looks like you are looking for slice method. It returns a new array that consists of a range of elements from the original array.

var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray.slice();
twoArray[0] = "z";             
trace(oneArray);  

EDIT: Note that slice does a shallow copy, not a deep copy. If you are looking for a deep copy then please follow the link specified in the comment.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • 5
    Here is some information on deep cloning http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee7.html – John Giotta Nov 18 '11 at 17:23
  • Thanks for answer, and especially the difference between shallow vs deep cloning (I had no idea). – ggkmath Nov 19 '11 at 17:12
6

You can clone the array to guarantee two seperate copies with the same values in each Array element:

var oneArray:Array = new Array("a", "b", "c");
var twoArray:Array = oneArray.concat();
twoArray[0] = "z";
trace(oneArray); // Output: a,b,c

Hope this is what you're looking for.

xLite
  • 1,441
  • 3
  • 15
  • 28
  • Just noticed taskinoor's answer, to pursuade you towards my direction, hehe, `concat` is actually twice as fast as `slice` in terms of cloning an Array. :) – xLite Nov 18 '11 at 23:59
  • 1
    Thanks! I researched a bit and found a few links supporting your claim. Here's one: http://agit8.turbulent.ca/bwp/2008/08/flash-as3-optimization-fastest-way-to-copy-an-array/ – ggkmath Nov 19 '11 at 17:13
  • 1
    this will only make a shallow copy. for a deep copy of an array you have to use ByteArray for cloning arrays in AS3: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee7.html – Chunky Chunk Nov 20 '11 at 14:40
0

If I understand the question correctly, you could do this:

var collection= new ArrayCollection(["a", "b", "c"]);
var clonedCollection = new ArrayCollection(ObjectUtil.copy(collection.source) as Array);

// a, b, c
trace(collection.toString());
// a, b, c
trace(clonedCollection .toString());

clonedCollection.removeItemAt(0);

// a, b, c
trace(collection.toString());
// b, c
trace(clonedCollection .toString());
tth
  • 703
  • 3
  • 8
  • 15
-2

You can create a clone function to copy the object using the ByteArray.writeObject and then out to a new object using ByteArray.readObject, as described in livedocs.adobe.com - Cloning arrays.

Note that writeObject and readObject will not understand objects more complex than Object and Array.

Tisho
  • 8,320
  • 6
  • 44
  • 52