O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required