views:

47

answers:

1
 var firstarray:Array = new Array();


 function traceArray(arr:Array){    
  for(var i:int = 0; i < arr.length; ++i) {
       trace(firstarray[i].matrix);    
  } 
 }



 for (var i:int = 0; i < 10; ++i) {   
  firstarray.push({ matrix:[1,0,0,1], prod:i}); 
 }

 var secondarray:Array = new Array();
 secondarray = firstarray;
 secondarray.push({ matrix:"hello" });

 traceArray(firstarray);

should the trace result be

1,0,0,1 1,0,0,1 1,0,0,1 1,0,0,1
1,0,0,1 1,0,0,1 1,0,0,1 1,0,0,1
1,0,0,1 1,0,0,1 hello

or

1,0,0,1 1,0,0,1 1,0,0,1 1,0,0,1
1,0,0,1 1,0,0,1 1,0,0,1 1,0,0,1
1,0,0,1 1,0,0,1

+3  A: 

it will output:

1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
1,0,0,1
hello

This is because you are setting secondarray to the same array reference as firstarray.

If you want to copy the contents of firstarray into secondarray use concat():

secondarray = firstarray.concat();

Actually, since you are using arrays in an array, you might have to loop each element and concat copy the contents of each matrix item. (Sorry for the edits, but I just remembered. ;))

Andir
hey... thanks for your answer
wee