29.04.2013 Views

ACTIONSCRIPT 3.0

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: Working with arrays<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Arrays allow you to store multiple values in a single data structure. You can use simple indexed arrays that store values<br />

using fixed ordinal integer indexes or complex associative arrays that store values using arbitrary keys. Arrays can also<br />

be multidimensional, containing elements that are themselves arrays. Finally, you can use a Vector for an array whose<br />

elements are all instances of the same data type.<br />

More Help topics<br />

Array<br />

Vector<br />

Basics of arrays<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Often in programming you’ll need to work with a set of items rather than a single object. For example, in a music player<br />

application, you might want to have a list of songs waiting to be played. You wouldn’t want to have to create a separate<br />

variable for each song on that list. It would be preferable to have all the Song objects together in a bundle, and be able<br />

to work with them as a group.<br />

An array is a programming element that acts as a container for a set of items, such as a list of songs. Most commonly<br />

all the items in an array are instances of the same class, but that is not a requirement in ActionScript. The individual<br />

items in an array are known as the array’s elements. You can think of an array as a file drawer for variables. Variables<br />

can be added as elements in the array, which is like placing a folder into the file drawer. You can work with the array<br />

as a single variable (like carrying the whole drawer to a different location). You can work with the variables as a group<br />

(like flipping through the folders one by one searching for a piece of information). You can also access them<br />

individually (like opening the drawer and selecting a single folder).<br />

For example, imagine you’re creating a music player application where a user can select multiple songs and add them<br />

to a playlist. In your ActionScript code, you have a method named addSongsToPlaylist(), which accepts a single<br />

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<br />

the addSongsToPlaylist() method only one time, passing it the array containing the Song objects. Inside the<br />

addSongsToPlaylist() method, you can use a loop to go through the array’s elements (the songs) one by one and<br />

actually add them to the playlist.<br />

The most common type of ActionScript array is an indexed array. In an indexed array each item is stored in a<br />

numbered slot (known as an index). Items are accessed using the number, like an address. Indexed arrays work well<br />

for most programming needs. The Array class is one common class that’s used to represent an indexed array.<br />

Often, an indexed array is used to store multiple items of the same type (objects that are instances of the same class).<br />

The Array class doesn’t have any means for restricting the type of items it contains. The Vector class is a type of indexed<br />

array in which all the items in a single array are the same type. Using a Vector instance instead of an Array instance<br />

can also provide performance improvements and other benefits. The Vector class is available starting with Flash Player<br />

10 and Adobe AIR 1.5.<br />

Last updated 4/22/2013<br />

25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!