AS3 's Array,object, the contrast of dictionary

Source: Internet
Author: User

Object, array, and dictionary are associative arrays, that is, "keys" to index Storage "value", is the "key-value" pairs of unordered collection.



1.Object
When object is used as an associative array, each property name of the generic object is treated as a key, providing access to the stored value, as shown in the following example:
[Plain] View plaincopy

var obj:object = {key1: "value1", Key2: "value2"};
Trace (obj["Key1"], obj["Key2"]); Output: value1 value2

You can also use the parentheses operator ([]) or the dot operator (.)-that is, dynamic properties to add values to the array:
[Plain] View plaincopy

var obj:object = new Object ();
obj["Key1"] = "value1"; Format error, do not use spaces
Obj.key2= "value2";
Trace (obj["Key1"], Obj.key2); Output: value1 value2

But if there is a space in the key, note that the space character can be used with the parentheses operator, but an error is generated when trying to use it with the dot operator, so it is not recommended to use spaces in the key name.



2.Array
Array cannot initialize the array with text, nor can it add elements through attributes, as in the following example:
[Plain] View plaincopy

var arr:array = new Array ();
arr["Key1"] = "value1";
arr["Key2"] = "value2";
Trace (arr["Key1"], arr["Key2"]); Output: value1 value2

There is no advantage in creating an associative array using the array constructor, and the key of the array must be a string type, preferably not an array of associative arrays.



3.Dictionary
Dictionary is an associative array with object keys, that is, an associative array that can use objects instead of strings as keys, example code:
[Plain] View plaincopy

var groupmap:dictionary = new Dictionary ();
The object to use as the key
var spr1:sprite = new Sprite ();
var spr2:sprite = new Sprite ();
var spr3:sprite = new Sprite ();
The object to use as a value
var groupa:object = new Object ();
var groupb:object = new Object ();
Creates a new key-value pair in the dictionary.
GROUPMAP[SPR1] = Groupa;
GROUPMAP[SPR2] = GroupB;
GROUPMAP[SPR3] = GroupB;

You can use the for: In loop or for each: In to iterate through the contents of the Dictionary object, the difference is that for: In loops directly accesses the object key of the Dictionary object, and for each: In Access is a value. You can also use the property access operator ([]) to access the value of the Dictionary object:
[Plain] View plaincopy

for (Var key:object in GroupMap)
{
Trace (key, Groupmap[key]);
}
/* Output:
[Object Sprite] [Object Object]
[Object Sprite] [Object Object]
[Object Sprite] [Object Object]
*/
For each (Var item:object in GroupMap)
{
Trace (item);
}
/* Output:
[Object Object]
[Object Object]
[Object Object]
*/

The way to delete dictionary is:
[Plain] View plaincopy

Delete Dic[key];
If key is an object, remember to release a reference to it
key = null;

AS3 's Array,object, the contrast of dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.