User Guide
227
8
CHAPTER 8
Working with Arrays
Arrays allow you to store multiple values in a single data structure. You can use simple indexed
arrays that store values using fixed ordinal integer indexes or complex associative arrays that
store values using arbitrary keys. Arrays can also be multidimensional, containing elements
that are themselves arrays. This chapter discusses how to create and manipulate various types
of arrays.
Contents
Indexed arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227
Associative arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
Multidimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Cloning arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243
Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243
Example: PlayList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
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. As the following code
shows, you can create an indexed array by calling the Array class constructor or by initializing
the array with an array literal:
// Use Array constructor.
var myArray:Array = new Array();
myArray.push("one");
myArray.push("two");
myArray.push("three");
trace(myArray); // output: one,two,three
// Use Array literal.