Indexed arrays

Indexed arrays store a series of one or more values organized such that each value can be accessed using an unsigned integer value. The first index is always the number 0, and the index increments by 1 for each subsequent element added to the array. In ActionScript 3.0, two classes are used as indexed arrays: the Array class and the Vector class.

Indexed arrays use an unsigned 32-bit integer for the index number. The maximum size of an indexed array is 232 - 1 or 4,294,967,295. An attempt to create an array that is larger than the maximum size results in a run-time error.

To access an individual element of an indexed array, you use the array access ( [] ) operator to specify the index position of the element you wish to access. For example, the following code represents the first element (the element at index 0) in an indexed array named songTitles :

songTitles[0]

The combination of the array variable name followed by the index in square brackets functions as a single identifier. (In other words, it can be used in any way a variable name can). You can assign a value to an indexed array element by using the name and index on the left side of an assignment statement:

songTitles[1] = "Symphony No. 5 in D minor";

Likewise, you can retrieve the value of an indexed array element by using the name and index on the right side of an assignment statement:

var nextSong:String = songTitles[2];

You can also use a variable in the square brackets rather than providing an explicit value. (The variable must contain a non-negative integer value such as a uint, a positive int, or a positive integer Number instance). This technique is commonly used to “loop over” the elements in an indexed array and perform an operation on some or all the elements. The following code listing demonstrates this technique. The code uses a loop to access each value in an Array object named oddNumbers . It uses the trace() statement to print each value in the form “oddNumber[ index ] = value ”:

var oddNumbers:Array = [1, 3, 5, 7, 9, 11]; 
var len:uint = oddNumbers.length; 
for (var i:uint = 0; i < len; i++) 
{ 
    trace("oddNumbers[" + i.toString() + "] = " + oddNumbers[i].toString()); 
}

The Array class

The first type of indexed array is the Array class. An Array instance can hold a value of any data type. The same Array object can hold objects that are of different data types. For example, a single Array instance can have a String value in index 0, a Number instance in index 1, and an XML object in index 2.

The Vector class

Another type of indexed array that’s available in ActionScript 3.0 is the Vector class. A Vector instance is a typed array , which means that all the elements in a Vector instance always have the same data type.

Note: The Vector class is available starting with Flash Player 10 and Adobe AIR 1.5.

When you declare a Vector variable or instantiate a Vector object, you explicitly specify the data type of the objects that the Vector can contain. The specified data type is known as the Vector’s base type . At run time and at compile time (in strict mode), any code that sets the value of a Vector element or retrieves a value from a Vector is checked. If the data type of the object being added or retrieved doesn’t match the Vector’s base type, an error occurs.

In addition to the data type restriction, the Vector class has other restrictions that distinguish it from the Array class:

  • A Vector is a dense array. An Array object may have values in indices 0 and 7 even if it has no values in positions 1 through 6. However, a Vector must have a value (or null ) in each index.

  • A Vector can optionally be fixed-length. This means that the number of elements the Vector contains can’t change.

  • Access to a Vector’s elements is bounds-checked. You can never read a value from an index greater than the final element ( length - 1). You can never set a value with an index more than one beyond the current final index. (In other words, you can only set a value at an existing index or at index [length] .)

As a result of its restrictions, a Vector has two primary benefits over an Array instance whose elements are all instances of a single class:

  • Performance: array element access and iteration are much faster when using a Vector instance than when using an Array instance.

  • Type safety: in strict mode the compiler can identify data type errors. Examples of such errors include assigning a value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector. At run time, data types are also checked when adding data to or reading data from a Vector object. Note, however, that when you use the push() method or unshift() method to add values to a Vector, the arguments’ data types are not checked at compile time. When using those methods the values are still checked at run time.

Aside from the additional restrictions and benefits, the Vector class is very much like the Array class. The properties and methods of a Vector object are similar—for the most part identical—to the properties and methods of an Array. In any situation where you would use an Array in which all the elements have the same data type, a Vector instance is preferable.