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.
There are two ways to create associative arrays in ActionScript 3.0. The first way is to use the Object constructor, which has the key 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 monitorInfo, using an object 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();
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 spacesmonitorInfo.colors = "16.7 million";
trace (monitorInfo["aspect ratio"], monitorInfo.colors);
// output: 16:10 16.7 million
Note that the key named aspect ratio contains a space character. This is possible with the 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 type and a key called resolution, along 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 Array.length property or any of the methods of the Array class with associative 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.

1 Comments:
I read your blog and thought I could tell you about something else which would be useful for seeing maps and directions in India.
I am writing to tell you about MapmyIndia.com, a free interactive maps and directions portal for all India. See the map of connaught place, new delhi, get directions in mumbai from nariman point to juhu airport, and find nearby ATMs in kormangala, bangalore.
As a company and individual enthusiasts, we dream only of solving the problem of reliable directions and navigation for India. For your blog specifically, you can map enable it by using our youtube-style embeddable maps, and links to specific searches (of maps, directions, local and eLocation) on MapmyIndia.
Do give us feedback, suggestions, or get involved yourself by mailing me back at tarun@mapmyindia.com or marketing@mapmyindia.com.
And if you find the different services useful, we would be grateful to you for writing and telling your readers about us.
Warm Regards,
Tarun Gupta
The MapmyIndia Team
For directions in India, just search print and go with MapmyIndia.com
Post a Comment
<< Home