AS3.0 miscellaneous--dictionary, object and array

Source: Internet
Author: User

Source: http://blog.csdn.net/m_leonwang/article/details/8811829

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
    1. var obj:object = {key1: "value1", Key2: "value2"};
    2. 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
    1. var obj:object = new Object ();
    2. obj["Key1"] = "value1"; Format error, do not use spaces
    3. Obj.key2= "value2";
    4. 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
    1. var arr:array = new Array ();
    2. arr["Key1"] = "value1";
    3. arr["Key2"] = "value2";
    4. 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
    1. var groupmap:dictionary = new dictionary ();   
    2. //  the object to use as the key   
    3. var spr1:sprite = new sprite ();   
    4. var spr2:sprite = new sprite ();   
    5. var spr3:sprite = new sprite ();   
    6. //  the object to use as a value   
    7. var groupa:object = new object ();   
    8. Var groupb:object = new object ();   
    9. //  create a new key-value pair in the dictionary.   
    10. groupmap[spr1] = groupa;  
    11. groupmap[spr2] =  groupb;  
    12. 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
    1. for  (var key:object in groupmap)   
    2. {   
    3. trace (Key, groupmap[key]);   
    4. }  
    5. /*  output:   
    6. [object sprite] [object object]  
    7. [OBJECT SPRITE] [OBJECT OBJECT]  
    8. [object sprite] [object object]  
    9. */  
    10. for each  (Var item:object in groupmap)   
    11. {  
    12.     trace (item);   
    13. }   
    14. /*  output:   
    15. [OBJECT OBJECT]  
    16. [ object object]  
    17. [OBJECT OBJECT]  
    18. */  

The way to delete dictionary is:

[Plain]View Plaincopy
      1. Delete Dic[key];
      2. If key is an object, remember to release a reference to it

AS3.0 miscellaneous--dictionary, object and array

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.