PDA

View Full Version : Array values mysteriously change



vurx
August 31st, 2008, 11:03 PM
the code below is a much simplified version but it duplicates the mysterious error. below i trace the contents of genesArray using a function and all of the cells are different, but when i trace it again they are all the same. i am truly baffled as to why.





package {
import flash.display.MovieClip;

public class GMachine extends MovieClip{

private var randomArray:Array;
private var total:int;
private var rndm:int;
private var genesArray:Array;


public function GMachine(){
randomArray = new Array();
genesArray = new Array();
total = 5;
makeGenes();
}
private function randomArrayGenerator():Array{
for(var i:int = 0; i < total; i++){
rndm = Math.random()*10;
randomArray[i] = rndm;
}
return randomArray;
}

private function makeGenes():void{
trace("here they are different");
for(var i:int = 0; i < total; i++){
genesArray[i] = randomArrayGenerator();
traceArray(genesArray[i]);
}
trace("why are these the same?");
for(i = 0; i < total; i++){
traceArray(genesArray[i]);
}
}

private function traceArray(inArray:Array):void{
var tmpStr:String = "";
for(var j:int = 0; j < inArray.length; j++){
tmpStr+=inArray[j]+" ";
}
trace(tmpStr);
}

}
}

sample output:



here they are different
9 0 2 6 5
6 1 0 5 9
7 4 1 1 7
3 3 0 6 0
0 9 7 4 6
why are these the same?
0 9 7 4 6
0 9 7 4 6
0 9 7 4 6
0 9 7 4 6
0 9 7 4 6

Krilnon
September 1st, 2008, 12:07 AM
You should declare randomArray inside of the generator. Before, you were changing/overwriting the same array each time.


private function randomArrayGenerator():Array {
var randomArray:Array = new Array();
for (var i:int = 0; i < total; i++) {
rndm = Math.random()*10;
randomArray[i] = rndm;
}
return randomArray;
}

vurx
September 1st, 2008, 04:21 AM
thanks Krilnon, that fixed it but im not exactly sure why. i had randomArray defined in the class and thought i was modifying that one. im purposely trying to reuse the same array, trying to keep objects to a minimum because performance is important.

im baffled as to why the values would read one way in the first trace and differently in the second. does it have to something with scope? im not sure if flash makes deep copies of arrays or just uses pointers.

might it be because im returning a value instead of accessing the modified one?

Krilnon
September 1st, 2008, 12:48 PM
You only create a new Array instance for randomArray once, so you are returning the same reference every time.

The values in the first trace change because you are changing the values in randomArray as you are looping. Later, you are no longer changing the values. In both loops, you are accessing the same array.

In short, you are only using the Array constructor twice and none of the methods that you are using use an Array constructor internally, so you can't expect to have more than two Array instances.

vurx
September 1st, 2008, 07:11 PM
after sleeping on it i can easily see the error. funny what too much coding can do to one's thought processes.

thanks again