Skip to main content
Participant
November 6, 2013
Answered

Trouble updating an entry in a matrix

  • November 6, 2013
  • 1 reply
  • 329 views

I am new in AS3, so please excuse a possibley easy question.

I want to update one entry in a matrix M. I use the commonsensical M[row][column]=1. It however has unexpected results:

The code is the following:

var m:Array = new Array()

var n:Array= new Array()

for (var i=0;i<3;i++) {

    n.push(0);

}

for (var j=0;j<3;j++) {

    m.push(n);

}

trace(m);

m[2][2]=34;

trace(m);

In the obtained matrix, more than one entry is updated and I obtain the following:

0,0,34,0,0,34,0,0,34

Any help?. Thanks

This topic has been closed for replies.
Correct answer Ned Murphy

m is storing references to n, not three separate arrays.  You'll get the same result if you change

m[2][2]=34;

to

n[2]=34;

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
November 6, 2013

m is storing references to n, not three separate arrays.  You'll get the same result if you change

m[2][2]=34;

to

n[2]=34;