Arrays are essential to successful ActionScript programming. At its most basic, an array can be thought of as a series of variables that can be referred to using a single, common name, which is often easier than having separate variable names for each datum. And what's more, the data in an array is ordered and enumeratable, providing a convenient way to organize, sort, and access the values.

Arrays provide a way of grouping together, organizing, and processing related data elements. The concept of an array should not be foreign to you. If fact, we use the concept of arrays in everyday life all the time. A simple grocery list or to-do list can be thought of as an array. Your address book is an array containing people's names and addresses. Libraries keep track of books using an indexing system whereby each book becomes, conceptually, an element in a library's array. You'll ordinarily use arrays to hold a series of related items, such as playing cards in a deck, as seen in Recipe 5.10.

In ActionScript, there are two kinds of arrays: integer-indexed and associative. Unless otherwise specified, the term "array" usually refers to an integer-indexed array. Both types of arrays group together related data, but they use different means of accessing the data. An integer-indexed array uses integers (numbers) as unique identifiers for each element in the array. Such arrays are ordered by index (i.e., element number), starting from 0. Each element occupies a numbered slot in the array. Integer-indexed arrays are ideal for sets of data that you want to work with in sequential order. Associative arrays use string keys to access each value. You can read more about associative arrays in Recipe 6.12.

Integer-indexed arrays are the focus of the majority of the recipes in this chapter, and so you should know how to create an array first. There are two ways to construct a new array in ActionScript: with the constructor function or as an array literal. All arrays are members of the Array class. You can use the Array( ) constructor function to instantiate new array objects in one of three ways:

myArray = new Array(  );          // Create an empty array.
myArray = new Array(numElems);  // Create an array with numElems undefined elements.
myArray = new Array(elem0,...elemn); // Create an array with specified elements.

The array literal notation also creates a new array but without having to use the constructor function. Literal notation is convenient for specifying elements at the time of creation, such as:

myArray = ["a", "b", "c"];

Some methods of the Array class modify the existing array on which the method is called, and others return a new array (offering an indirect way to create arrays).

You can retrieve and set array elements using the array-access operator (square brackets) and the index of the element you wish to get or set. For example:

// Set the fifth element of the array to "apples" (array indexes start at 0).
myArray[4] = "apples";

// Display the fifth element in the Output window.
trace(myArray[4]);   // Displays: apples

ActionScript doesn't care what kind of values you store in arrays. You can store strings, numbers, Booleans, or any kind of objects. And, unlike stricter programming languages, you can even store different datatypes in a single array. For example, this array stores a string, an integer, a Boolean, and an object:

myArray = ["a", 2, true, new Object(  )];

Unlike many languages, ActionScript doesn't require you to specify the number of elements in an array when it is declared (a.k.a. dimensioned or allocated).

    Part I: Local Recipes
    Part II: Remote Recipes