Associative Arrays - Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual

Programming actionscript 3.0
Table of Contents

Advertisement

One issue to be aware of with the
method is that any nested arrays are always returned
join()
with comma-separated values, no matter what separator you specify for the main array
elements, as the following example shows:
var nested:Array = ["b","c","d"];
var letters:Array = ["a",nested,"e"];
var joined:String = letters.join("+");
trace(joined); // output: a+b,c,d+e

Associative arrays

Associative arrays, which are sometimes called hashes or maps, use keys instead of a numeric
index to organize stored values. Each key in an associative array is a unique string that is used
to access a stored value. An associative array is an instance of the Object class, which means
that each key corresponds to a property name. Associative arrays are unordered collections of
key and value pairs. Your code should not expect the keys of an associative array to be in a
specific order.
ActionScript 3.0 introduces an advanced type of associative array called a dictionary.
Dictionaries, which are instances of the Dictionary class in the flash.utils package, use keys
that can be of any data type but are usually instances of the Object class. In other words,
dictionary keys are not limited to values of type String.
This section describes how to create associative arrays that use strings for keys and how to use
the Dictionary class.
Associative arrays with string keys
There are two ways to create associative arrays in ActionScript 3.0. The first way is to use the
Object constructor, which has the advantage of allowing you to initialize your array with an
object literal. An instance of the Object class, also called a generic object, is functionally
identical to an associative array. Each property name of the generic object serves as the key
that provides access to a stored value.
The following example creates an associative array named
, using an object
monitorInfo
literal to initialize the array with two key and value pairs:
var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
trace (monitorInfo["type"], monitorInfo["resolution"]);
// output: Flat Panel 1600 x 1200
If you do not need to initialize the array at declaration time, you can use the Object
constructor to create the array, as follows:
var monitorInfo:Object = new Object();
236
Working with Arrays

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents