Flash 5
Creating multidimensional array in Flash 5 is not all that obvious. The
trick is to create one dimensional array first, then for
each element of the array, assign another array object. In general,
the technique is as follows:
To create:
arrayName[dimension1][dimension2] ... [dimensionX]
Do this:
arrayName=new Array(dimension1);
for (i=0; i<dimension1; i++)
{
arrayName[i]=new Array(dimension2);
for (j=0; j<dimension2; j++)
{
....
for (k=0; k<dimensionX; k++)
{
arrayName[i][j]...[k]=new Array(dimensionX);
}
}
}
|
For example, to create a 4 by 4 array called mMatrix:
mMatrix[0][0] |
mMatrix[0][1] |
mMatrix[0][2] |
mMatrix[0][3] |
mMatrix[1][0] |
mMatrix[1][1] |
mMatrix[1][2] |
mMatrix[1][3] |
mMatrix[2][0] |
mMatrix[2][1] |
mMatrix[2][2] |
mMatrix[2][3] |
mMatrix[3][0] |
mMatrix[3][1] |
mMatrix[3][2] |
mMatrix[3][3] |
Do it in this way:
this.mMatrix=new Array(4);
for (i=0; i<4; i++)
{
this.mMatrix[i]=new Array(4);
}
|
To confirm that this actually works, let's assign values to each element:
this.mMatrix=new Array(4);
for (i=0; i<4; i++)
{
this.mMatrix[i]=new Array(4);
for (j=0; j<4; j++)
{
this.mMatrix[i][j]="" + i + j; // assign values
}
}
|
Which, Produces the following.
You can see that this indeed works, and the array can be accessed as usual
by
arrayName[index1][index2]
|
<<ARRAY
CODE GENERATOR>>
<<INDEX>>
(C) 2001 F. Permadi
Oct 2001
Terms
of Use