Multi-Dimensional Arrays in Flash

Arrays make a great way to store and manage data in actionscript. However, sometimes just a single dimension may not be adequate for some situations such as when storing table data. Then you will need to either: define an array for each column for every row, or just make a one two-dimensional array.

A two-dimensional array can be thought of an array nested in an array. To instantiate an array, we can either do:

var arr1:Array = new Array(1, 2, 3);
var arr2:Array = new Array(4, 5, 6);
var arr3:Array = new Array(7, 8, 9);
var arr:Array = new Array(arr1, arr2, arr3);

Or the quicker way:

var arr:Array = new Array([1, 2, 3], [4, 5, 6], [7, 8, 9]);

All we're doing here is defining arrays inside of an array. To reference a two-dimensional array is practically the same as a normal array except for we have a second element to reference. This means we just need to add another "[n-1]:"

// Output: 6

To add a new element to the first array, we can use the same syntax as we would with a single-dimension array, but we would be pushing an array data type:

// Adds another element to the outer array

To add an element to one of our existing arrays, we reference the array's element in the outer array:

// Ads a sub element to arr1[1] (= 4,5,6)

The rest of the functions in the Array package are used in the same way, but with different references to the caller.

Posted by dank

Comments (4) Trackbacks (0)
  1. could u add an item to that array during running program

  2. Hi — I call a function that returns a 2d array, stuck on how to setup the array variable that catches the returned 2nd array.

    var getwordboxes:Responder = new Responder(onResult2dArray, onFault);
    //var thisTestPage:Array=[];
    //var thisMainPage:Array=
    gw.call(“navigation.get_word_boxes”, getwordboxes, 1);

    function onResult2dArray(responds:Array):void {

    // responds should catch the result but have not setup the 2d array correctly.

  3. Have you checked to see if this will run with a different variable type? I’m not too familiar with using Media Server, but from the documentation, this appears to be ok. I would check the documentation for the NetConnection class and double check that everything is in sync between the server and client.

Leave a comment

No trackbacks yet.