Basics of arrays
Introduction to working with arrays
Often in programming you’ll need to work with a set of items rather than a single object. For example, in a music player application, you might want to have a list of songs waiting to be played. You wouldn’t want to have to create a separate variable for each song on that list. It would be preferable to have all the Song objects together in a bundle, and be able to work with them as a group.
An array is a programming element that acts as a container for a set of items, such as a list of songs. Most commonly all the items in an array are instances of the same class, but that is not a requirement in ActionScript. The individual items in an array are known as the array’s elements. You can think of an array as a file drawer for variables. Variables can be added as elements in the array, which is like placing a folder into the file drawer. You can work with the array as a single variable (like carrying the whole drawer to a different location). You can work with the variables as a group (like flipping through the folders one by one searching for a piece of information). You can also access them individually (like opening the drawer and selecting a single folder).
For example, imagine you’re creating a music player application where a user can select multiple songs and add them to a playlist. In your ActionScript code, you have a method named addSongsToPlaylist(), which accepts a single array as a parameter. No matter how many songs you want to add to the list (a few, a lot, or even only one), you call the addSongsToPlaylist() method only one time, passing it the array containing the Song objects. Inside the addSongsToPlaylist() method, you can use a loop to go through the array’s elements (the songs) one by one and actually add them to the playlist.
The most common type of ActionScript array is an indexed array. In an indexed array each item is stored in a numbered slot (known as an index). Items are accessed using the number, like an address. Indexed arrays work well for most programming needs. The Array class is one common class that’s used to represent an indexed array.
Often, an indexed array is used to store multiple items of the same type (objects that are instances of the same class). The Array class doesn’t have any means for restricting the type of items it contains. The Vector class is a type of indexed array in which all the items in a single array are the same type. Using a Vector instance instead of an Array instance can also provide performance improvements and other benefits. The Vector class is available starting with Flash Player 10 and Adobe AIR 1.5.
A special use of an indexed array is a multidimensional array. A multidimensional array is an indexed array whose elements are indexed arrays (which in turn contain other elements).
Another type of array is an associative array, which uses a string key instead of a numeric index to identify individual elements. Finally, ActionScript 3.0 also includes the Dictionary class, which represents a dictionary. A dictionary is an array that allows you to use any type of object as a key to distinguish between elements.
Common array tasks
The following common activities for working with arrays are described in this chapter:
Important concepts and terms
The following reference list contains important terms that you will encounter in this chapter: