Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About arrays > Creating multidimensional arrays | |||
In ActionScript, you can implement arrays as nested arrays that are essentially arrays of arrays. Nested arrays, also known as multidimensional arrays, can be thought of as matrices or grids. Therefore, when you are programming, you might use multidimensional arrays to model these kinds of structures. For example, a chess board is a grid of eight columns and rows; you could model the chess board as an array that contains eight elements, each of which is also an array that contains eight elements.
For example, consider a list of tasks that is stored as an indexed array of strings:
var tasks:Array = ["wash dishes", "take out trash"];
If you want to store a separate list of tasks for each day of the week, you can create a multidimensional array with one element for each day of the week. Each element contains an indexed array that stores the list of tasks.
|
CAUTION |
When you use the array access operator, the ActionScript compiler cannot check whether the accessed element is a valid property of the object. |
To create a basic multidimensional array and retrieve elements from the array:
var twoDArray:Array = new Array(new Array("one","two"), new Array("three", "four"));
trace(twoDArray);
This array, twoDArray, consists of two array elements. These elements are themselves arrays consisting of two elements. In this case, twoDArray is the main array that contains two nested arrays.
one,two,three,four
trace statement, as shown below:
// trace(twoDArray);
trace(twoDArray[0][0]); // one trace(twoDArray[1][1]); // four
To retrieve elements of a multidimensional array, you use multiple array access ([]) operators after the name of the top-level array. The first [] refers to the index of the top-level array. Subsequent array access operators refer to elements of nested arrays.
one four
You can use nested for loops to create multidimensional arrays. The next example shows you how.
To create a multidimensional array using a for loop:
var gridSize:Number = 3;
var mainArr:Array = new Array(gridSize);
var i:Number;
var j:Number;
for (i = 0; i < gridSize; i++) {
mainArr[i] = new Array(gridSize);
for (j = 0; j < gridSize; j++) {
mainArr[i][j] = "[" + i + "][" + j + "]";
}
}
trace(mainArr);
This ActionScript creates a 3 x 3 array and sets the value of each array node to its index. Then you trace the array (mainArr).
You see the following display in the Output panel:
[0][0],[0][1],[0][2],[1][0],[1][1],[1][2],[2][0],[2][1],[2][2]
You can also use nested for loops to iterate through the elements of a multidimensional array, as shown in the next example.
To use a for loop to iterate a multidimensional array:
// from previous example
var gridSize:Number = 3;
var mainArr:Array = new Array(gridSize);
var i:Number;
var j:Number;
for (i = 0; i < gridSize; i++) {
mainArr[i] = new Array(gridSize);
for (j = 0; j < gridSize; j++) {
mainArr[i][j] = "[" + i + "][" + j + "]";
}
}
In this code, seen in the previous example, the outer loop iterates through each element of mainArray. The inner loop iterates through each nested array and outputs each array node.
// iterate through elements
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]);
}
}
This ActionScript iterates through the elements of the array. You use the length property of each array as the loop condition.
[0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]
For information on using arrays, see Using arrays. For information on array elements, see About adding and removing elements. For information on the array access operator, see Using dot and array access operators.
|
|
|
|