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:

  1. Create a new Flash document, and save it as multiArray1.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    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.

  3. Select Control > Test Movie to test the code. You see the following display in the Output panel.
    one,two,three,four
    
  4. Return to the authoring tool and open the Actions panel. Comment out the trace statement, as shown below:
    // trace(twoDArray);
    
  5. Add the following ActionScript at the end of your code on Frame 1 of the Timeline:
    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.

  6. Select Control > Test Movie to test the code. You see the following display in the Output panel.
    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:

  1. Create a new Flash document, and save it as multiArray2.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    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).

  3. Select Control > Test Movie to test the code.

    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:

  1. Create a new Flash document, and save it as multiArray3.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    // 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.

  3. Add the following ActionScript to Frame 1 of the Timeline, following the code you entered in step 2:
    // 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.

  4. Select Control > Test Movie to view the elements that are displayed in the Output panel. You will see the following in the Output panel:
    [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.