Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About arrays > Creating associative arrays | |||
An associative array, which is like an object, is made of unordered keys and values. Associative arrays use keys instead of a numeric index to organize stored values. Each key is a unique string, and it is associated with and used to access one value. That value can be a data type such as Number, Array, Object, and so on. When you create code to find a value that's associated with a key, you are indexing or performing a lookup. This is what you will probably use associative arrays for most often.
The association between a key and value is commonly referred to as its binding; the key and value are mapped to each other. For example, a contact book might be considered an associative array, where the names are the keys and email addresses are the values
|
NOTE |
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. |
When you use associative arrays, you can call the array element you need using a string rather than a number, which is often easier to remember. The downside is that these arrays aren't as useful in a loop because they do not use numbers as the index value. They are useful when you need to look up by key values frequently. For example, if you had an array of names and ages that you needed to refer to a lot, you might use an associative array.
The following example demonstrates how to create an object and define a series of properties in an associative array.
To create a simple associative array:// Define the object to use as an associative array. var someObj:Object = new Object(); // Define a series of properties. someObj.myShape = "Rectangle"; someObj.myW = 480; someObj.myH = 360; someObj.myX = 100; someObj.myY = 200; someObj.myAlpha = 72; someObj.myColor = 0xDFDFDF; // Display a property using dot operator and array access syntax. trace(someObj.myAlpha); // 72 trace(someObj["myAlpha"]); // 72
The first line of ActionScript defines a new object (someObj) that you use as the associative array. Following this, you define a series of properties in someObj. Finally, you display a property that you select using both dot operator and array access syntax.
|
NOTE |
You can access variables in an associative array using two different methods: dot syntax ( |
The Output panel displays the number 72 twice, which represents both of the alpha levels that you traced.
There are two ways to create associative arrays in ActionScript 2.0:
Both of these ways are demonstrated in upcoming examples.
|
NOTE |
The previous example used an Object constructor to create an associative array. |
If you use the Object constructor to create an associative array, you can take advantage of initializing your array with an object literal. An instance of the Object class, also called a generic object, is functionally identical to an associative array. In fact, Object instances are essentially associative arrays. You might use associative arrays for dictionary-like functionality, when it's more convenient to have string keys rather than numerical indices. Each property name of the generic object serves as the key that provides access to a stored value. For more information on literals, see About literals. For more information on classes, see Classes.
To create an associative array using an Object constructor:
var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
trace(monitorInfo["type"] + ", " + monitorInfo["resolution"]);
This code creates an associative array called monitorInfo, and uses an object literal to initialize the array with two key/value pairs.
|
NOTE |
If you do not need to initialize the array at declaration time, you can use the Object constructor to create the array: |
var monitorInfo:Object = new Object();
The Output panel displays the following text:
Flat Panel, 1600 x 1200
monitorInfo["aspectRatio"] = "16:10"; monitorInfo.colors = "16.7 million"; trace(monitorInfo["aspectRatio"] + ", " + monitorInfo.colors);
After you use using either an object literal or the Object class constructor to create the array, you can add new values to the array using either the bracket operator ([]) or the dot operator (.), as demonstrated in this code. The code you just typed adds two new values to monitorInfo array.
The Output panel displays the following text:
16:10, 16.7 million
Note that a key can contain a space character. This is possible with the bracket operator, but generates an error if you attempt this with the dot operator. Using spaces in your key names is not recommended. For more information on bracket operators and dot operators, see About operators. For more information on well-formatted code, see Formatting ActionScript syntax.
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.
|
NOTE |
There is no advantage to using the Array constructor to create an associative array. The Array constructor is best for creating indexed arrays. |
The next example demonstrates how to use the Array constructor to create an associative array.
To create an associative array using the Array constructor:var monitorInfo:Array = new Array(); monitorInfo["type"] = "Flat Panel"; monitorInfo["resolution"] = "1600 x 1200"; trace(monitorInfo["type"] + ", " + monitorInfo["resolution"]);
This code creates an associative array named monitorInfo using the Array constructor and adds a key called type and a key called resolution, along with their values.
The Output panel displays the following text:
Flat Panel, 1600 x 1200
|
NOTE |
There is no advantage to using the Array constructor to create an associative array. The Array constructor is best for creating indexed arrays. |
Associative arrays are essentially instances of the Object class, and there is no advantage of creating associative arrays using the Array constructor. Even though you create an associative array using the new Array() constructor, you cannot use any of the Array class's methods and properties (such as sort() or length) when using an associative array. If you want to use key/value pairs instead of a numeric index, you should use the Object class instead of an associative array.
|
|
|
|