[AS3]Array,Vector,关联数组,Dictionary,多维数组示例及说明
[AS3]Array,Vector,关联数组,Dictionary,多维数组示例及说明
[AS3]Array,Vector,关联数组,Dictionary,多维数组示例及说明
- //Array为任意类型的数组
- var names:Array = new Array(3);
- var oddNumbers:Array = [1, 3, 5, 7, 9, 11];
- var names:Array = new Array("John","Jane","David");
- //Vector为指定类型的数组,性能比Array快,类型检查
- var v:Vector.<String> = new Vector.<String>();
- var v:Vector.<int> = new <int>[0,1,2,]; //尾部逗号为可选,数组中不支持空项
- var friends:Vector.<String> = Vector.<String>(["Bob", "Larry", "Sarah"]); //Vector.<T>() 全局函数
- //数组插入元素
- var planets:Array = new Array();
- planets.push("Mars"); //在末尾添加
- planets.unshift("Mercury"); // 在开头添加
- planets.splice(1, 0, "Venus", "Earth"); //在指定索引位添加
- //检索和删除数组元素
- var oceans:Array = ["Victoria", "Pacific", "Aral", "Superior", "Indian", "Huron"];
- var first:String = oceans[0]; //检索元素
- oceans.pop(); //删除末尾一个元素
- oceans.shift(); //删除开头一个元素
- oceans.splice(startIndex, deleteCnt, "Arctic", "Atlantic"); //从指定索引位开始,删除指定长度的元素,并添加后两项字符串
- //排序
- oceans.reverse(); //反转
- oceans.sort(); //按预定义方式排序
- oceans.sort(Array.CASEINSENSITIVE); // Array.DESCENDING | Array.CASEINSENSITIVE
- oceans.sortOn(); //按某属性排序
- //查询数组
- //方法concat()、join() 、slice() 和 toString()
- //关联数组
- var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
- var tp:String =monitorInfo["type"];
- var monitorInfo:Array = new Array();
- monitorInfo["type"] = "Flat Panel";
- monitorInfo["resolution"] = "1600 x 1200";
- //字典
- import flash.display.Sprite;
- import flash.utils.Dictionary;
- var groupMap:Dictionary = new Dictionary();
- // objects to use as keys
- var spr1:Sprite = new Sprite();
- var spr2:Sprite = new Sprite();
- var spr3:Sprite = new Sprite();
- // objects to use as values
- var groupA:Object = new Object();
- var groupB:Object = new Object();
- // Create new key-value pairs in dictionary.
- groupMap[spr1] = groupA;
- groupMap[spr2] = groupB;
- groupMap[spr3] = groupB;
- if (groupMap[spr1] == groupA)
- {
- trace("spr1 is in groupA");
- }
- if (groupMap[spr2] == groupB)
- {
- trace("spr2 is in groupB");
- }
- if (groupMap[spr3] == groupB)
- {
- trace("spr3 is in groupB");
- }
- //循环访问
- for (var key:Object in groupMap)
- {
- trace(key, groupMap[key]);
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...