Search Flex Samples

flex 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:

  • Creating indexed arrays
  • Adding and removing array elements
  • Sorting array elements
  • Extracting portions of an array
  • Working with associative arrays and dictionaries
  • Working with multidimensional arrays
  • Copying array elements
  • Creating an array subclass

Important concepts and terms

The following reference list contains important terms that you will encounter in this chapter:

  • Array: An object that serves as a container to group multiple objects together.
  • Associative array: An array that uses string keys to identify individual elements.
  • Dictionary: An array whose items consist of pairs of objects, known as the key and the value. The key is used instead of a numeric index to identify a single element.
  • Element: A single item in an array.
  • Index: The numeric "address" used to identify a single element in an indexed array.
  • Indexed array: The standard type of array that stores each element in a numbered element, and uses the number (index) to identify individual elements.
  • Key: The string or object used to identify a single element in an associative array or a dictionary.
  • Multidimensional array: An array containing items that are arrays rather than single values.


Related Flex Samples

Learn Flex: Flex Samples | Flex Video Tutorials Flex Examples