Chapter 11. Arrays
Back in
Chapter 3, we learned that primitive
datatypes—strings, numbers, Booleans, null
,
and undefined
—represent basic information in
our scripts. We also learned that ActionScript supports several
composite datatypes, which can group several
pieces of data together into a single datum.
The array type is the first composite datatype we’ll study. Arrays are used to store and manipulate ordered lists of information and are, therefore, fundamental tools in sequential, repetitive programming. We use them to do everything from storing values retrieved via a user-input form, to generating pull-down menus, to keeping track of enemy spacecraft in a game. In its most basic form, an array is just a list of items, like your grocery list or the entries in your checkbook ledger.
What Is an Array?
An array is a data structure that can encompass multiple individual data values, just like a building is a physical structure encompassing multiple floors. Unlike a primitive datatype, an array can include more than one data value. Here is a simple example showing first, two strings, and then an array that contains two strings:
"oranges" // A single primitive string value "apples" // Another primitive string value ["oranges", "apples"] // A single array containing two strings
An array is a general-purpose container. It can contain any number of items and even items of different types. An array can even contain other arrays. Here is a simple example showing an array containing both strings ...
Get ActionScript: The Definitive Guide 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.