Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 237

Programming actionscript 3.0
Table of Contents

Advertisement

After the array is created using either an object literal or the Object class constructor, you can
add new values to the array using either the bracket operator (
) or the dot operator (
). The
[]
.
following example adds two new values to
:
monitorArray
monitorInfo["aspect ratio"] = "16:10"; // bad form, do not use spaces
monitorInfo.colors = "16.7 million";
trace (monitorInfo["aspect ratio"], monitorInfo.colors);
// output: 16:10 16.7 million
Note that the key named
contains a space character. This is possible with the
aspect ratio
bracket operator, but generates an error if attempted with the dot operator. Using spaces in
your key names is not recommended.
The second way to create an associative array is to use the Array constructor and then use
either the bracket operator (
) or the dot operator (
) to add key and value pairs to the array.
[]
.
If you declare your associative array to be of type Array, you cannot use an object literal to
initialize the array. The following example creates an associative array named
monitorInfo
using the Array constructor and adds a key called
and a key called
, along
type
resolution
with their values:
var monitorInfo:Array = new Array();
monitorInfo["type"] = "Flat Panel";
monitorInfo["resolution"] = "1600 x 1200";
trace(monitorInfo["type"], monitorInfo["resolution"]);
// output: Flat Panel 1600 x 1200
There is no advantage in using the Array constructor to create an associative array. You cannot
use the
property or any of the methods of the Array class with associative
Array.length
arrays, even if you use the Array constructor or the Array data type. The use of the Array
constructor is best left for the creation of indexed arrays.
Associative arrays with object keys
You can use the Dictionary class to create an associative array that uses objects for keys rather
than strings. Such arrays are sometimes called dictionaries, hashes, or maps. For example,
consider an application that determines the location of a Sprite object based on its association
with a specific container. You can use a Dictionary object to map each Sprite object to a
container.
Associative arrays
237

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents