A array is an object used to hold organized data. Each piece of data in an array is called an element and is refered to by an index.
To create multidimentional arrays in flash you must create arrays of arrays or "nested" arrays. In these multidimentional arrays the elements of the first array are also arrays that contain elements themselves. The first element is refered to by the index [0][0] the second element [0][1] the third [1][0] and fourth [1][1].
To create a mutli dimentional array you can use the "New Array" constructor.
var name_of_2d_array:Array = new
Array();
However you must construct the new array with other arrays as elements in this array so each must be declared individually..
var name_of_2d_array:Array =
new Array (New Array (), New Array ());
To enter data into an array you must allocate each piece of data to a specific element using the index notation.
name_of_2d_array = New Array( New Array ("value1","value2"), New Array ("value3","value4"));
This method is the same as the first the only difference is the syntax "the way it is written" as far as flash is concerned the two actionscript statements are identicle.
Multi-dimentional arrays are also known as matrices or grids
Larger multidimentional arrays can get big very quickly making it time consuming to enter and retrive data unless you use some sort of automated method to visit each element in the array. One such mehtod of diing this is to create a nested for loop.
for example:
var matrixSize:Number = 3;
var mainArr:Array = new Array(matrixSize);
var i:Number;
var j:Number;
for (i = 0; i < matrixSize; i++) {
mainArr[i] = new Array(matrixSize);
for (j = 0; j < matrixSize; j++) {
mainArr[i][j] = "[" + i + "][" + j + "]";
}
}
This code cycles thtough each element of each array and assigns it a value equal to the idex that describes it.
When the content of the multidimentional array is traced using the code:
trace(mainArr);
The following values appear: [0][0],[0][1],[0][2],[1][0],[1][1],[1][2],[2][0],[2][1],[2][2]
var outerArrayLength:Number = mainArr.length;
for (i = 0; i < outerArrayLength; i++) {
var innerArrayLength:Number = mainArr[i].length;
for (j = 0; j < innerArrayLength; j++) {
trace(mainArr[i][j]);
}
}
|