Chapter 11. Arrays

Arrays store and manipulate ordered lists of information and are, therefore, a fundamental tool in sequential, repetitive programming. We use arrays to do everything from storing user input, to generating pull-down menus, to keeping track of enemy spacecraft in a game. Practically speaking, an array is just a list of items, like your grocery list or the entries in your checkbook ledger. The items just happen to be ActionScript values.

What Is an Array?

An array is a data structure that can encompass multiple individual data values in an ordered list. Here is a simple example showing two separate strings, followed by an array that contains two strings:

"cherries"             // A single string
"peaches"              // Another string
["oranges", "apples"]  // A single array containing two strings

An array can contain any number of items, including items of different types. An array can even contain other arrays. Here is a simple example showing an array that contains both strings and numbers. It might represent your shopping list, showing how many of each item you intend to buy:

["oranges", 6, "apples", 4, "bananas", 3];

Though an array can keep track of many values, it’s important to recognize that the array itself is a single data value. Arrays are represented as instances of the Array class. As such, an array can be assigned to a variable or used as part of a complex expression:

// Assign an array to a variable var product:Array = ["ladies downhill skis", 475]; // Pass that array to a function ...

Get Essential ActionScript 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.