As3.0中的Dictionary使用测试

代码如下:

package
{
    import flash.display.Sprite;
    import flash.utils.Dictionary;
    
    public class AsTestDemo extends Sprite
    {
        public function AsTestDemo()
        {
            //类似于Java中的Map
            var dict:Dictionary = new Dictionary();
            
            //Dictionary强大之处
            dict[3.01] = "vokie";
            dict["123"] = -100;
            dict[123] = 100;
            dict[8] = 2.11;
            
            trace(dict[3.01]);
            trace(dict.hasOwnProperty(3+0.01)); //Java.Map.ContainsKey
            trace(dict.hasOwnProperty("3.01"));
            trace(dict.hasOwnProperty(123));
            trace(dict.hasOwnProperty(1234));
            trace("--------------------");
            for(var s1:String in dict)  //for(in)语句迭代key
            {
                trace("key:"+s1);
            }
            trace("--------------------");
            for each(var s2:String in dict) //for each(in)语句迭代value
            {
                trace("value:"+s2);
            }
        }
    }
}


运行结果:

vokie
true
true
true
false
--------------------
key:8
key:123
key:3.01
--------------------
value:2.11
value:100
value:vokie


测试结果中的键值覆盖,以及key、value的类型很随意,无需一一对应,使用的时候需要注意

原文地址:https://www.cnblogs.com/vokie/p/3602070.html