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 will be 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 might think of an array as a file drawer for variables. Variables can be added as elements in the array, which is like putting a folder into the file drawer. Once several files are in the 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); or you can 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 might have a method named addSongsToPlaylist(), which accepts a single array as a parameter. No matter how many songs are to be added to the list (a few, a lot, or even only one), you have to 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, which is an array where each item is stored in a numbered slot (known as an index), and items are accessed using the number, like an address. The Array class is used to represent an indexed array. Indexed arrays work well for most programming needs. A special use of an indexed array is a multidimensional array, which 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, for advanced users, ActionScript 3.0 also includes the Dictionary class, which represents a dictionary--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:

Working through in-chapter examples

As you're working through the chapter, you may want to test some of the example code listings for yourself. Essentially all the code listings in this chapter include the appropriate trace() function call. To test the code listings in this chapter:

  1. Create an empty Flash document.
  2. Select a keyframe in the timeline.
  3. Open the Actions panel and copy the code listing into the Script pane.
  4. Run the program using Control > Test Movie.

    You will see the results of the trace() function in the Output panel.

This and other techniques for testing example code listings are described in detail in Testing in-chapter example code listings.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000088.html