Using arrays

There are several different ways you can use arrays in your work. You can use them to store lists of objects, such as a bunch of returned items. If you load data from remote web servers, you might even receive data as an array of nested objects. Often, arrays contain data in a similar format. For example, if you build an audio application in Flash, you might have a user's playlist stored as an array of song information, stored in objects. Each object contains the song name, artist name, song duration, location of a sound file (such as an MP3), or any other information that you might need to associate with a particular file.

The location of an item in an array is called the index. All arrays are zero-based, which means that the first element in the array is [0], the second element is [1], and so on.

There are different kinds of arrays, which you'll discover in the following sections. The most common arrays use a numerical index to look up a particular item in an indexed array. The second kind of array is called an associative array and uses a text index instead of a numerical index to look up information. For more information on common arrays, see About arrays. For more information on associative arrays, see Creating associative arrays. For more information on multidimensional arrays, see Creating multidimensional arrays. For information on the array access operator, see Using dot and array access operators.

The built-in Array class lets you access and manipulate arrays. To create an Array object, you use the constructor new Array() or the array access operator ([]). To access the elements of an array, you also use the array access ([]) operator. The next example uses an indexed array.

To use arrays in your code:

  1. Create a new Flash document, and save it as basicArrays.fla.
  2. Add the following ActionScript to Frame 1 of the Timeline:
    // define a new array
    var myArr:Array = new Array();
    // define values at two indexes
    myArr[1] = "value1";
    myArr[0] = "value0";
    // iterate over the items in the array
    var i:String;
    for (i in myArr) {
        // trace the key/value pairs
        trace("key: " + i + ", value: " + myArr[i]);
    }
    

    In the first line of ActionScript, you define a new array to hold the values. Then, you define data (value0 and value1) at two indexes of the array. You use a for..in loop to iterate over each of the items in that array and display the key/value pairs in the Output panel using a trace statement.

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

    The following text is displayed in the Output panel:

    key: 0, value: value0
    key: 1, value: value1
    

    For more information on for..in loops, see Using for..in loops.

For information on how to create different kinds of arrays, see the following sections:

For a sample source file, array.fla, that illustrates array manipulation using ActionScript, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Arrays folder to access the sample. The code in the sample creates an array and sorts, adds, and removes items of two List components.