[AS3]as3合并一维数组和二维数组
[AS3]as3合并一维数组和二维数组,比较简单,自己先存着,以免以后用来
合并一维数组和二维数组,哈哈.比较简单,自己先存着,以免以后用来
如:
- var a1:Array=[1,2,3,4]
- var a2:Array=[432,4,5,7]
- var a3:Array=[36]
- var aa:Array=[a1,a2,a3]
- var m:mergeArray=new mergeArray;
- //var a:Array=m.Merge(aa);//-----------1
- var a:Array=m.Merge(a1,a2,a3)//--------2
- trace(a + " = " + a.length);
- //1,2都输入1,2,3,4,432,4,5,7,36 = 9
- package
- {
- /**
- * ...合并多个数组,可合并一维数组和二维数组
- * @author zkl CuPlayer.com
- */
- public class mergeArray
- {
- private var i:int;
- private var j:int;
- public function Merge(...args):Array
- {
- var _newArray:Array = new Array;
- var len:int = args.length;
- if (len == 1) {
- //二维数组
- if (args[0] is Array) {
- for (i = 0; i < args[0].length; i++ ) {
- //合并一维数组
- _newArray = mergeing(args[0]);
- }
- }
- }else {
- _newArray = mergeing(args);
- }
- return _newArray;
- }
- /**
- * 合并一维数组
- */
- private function mergeing(a1:Array):Array
- {
- var a:Array = new Array;
- for (i = 0; i < a1.length; i++ ) {
- if (a1[i] is Array) {
- for (j = 0; j < a1[i].length; j++ ) {
- a.push(a1[i][j]);
- }
- }
- }
- return a;
- }
- }
- }
升级版,不知还有没有更好的办法?
- package {
- /**
- * ...合并多个数组,可合并一维数组和二维数组
- * @author zkl CuPlayer.com
- * 2010-8-30 14:27
- */
- public class mergeArrayOk {
- public function Merge(... args):Array {
- return merge1(args);
- }
- private function merge1(arr:Array):Array {
- var i:int, j:int;
- var _tempArray:Array = new Array;
- //为了不让每次循环都计算数组长度
- var len:int = arr.length;
- for (i = 0; i < len; i++){
- if (arr[i] is Array){
- var len2:int = arr[i].length;
- for (j = 0; j < len2; j++){
- if (arr[i][j] != null){
- _tempArray.push(arr[i][j]);
- }
- }
- } else if (arr[i] != null){
- _tempArray.push(arr[i]);
- }
- }
- //查询_tempArray是否还有数组,有则再次转换
- var m:int;
- var len3:int = _tempArray.length;
- for (i = 0; i < len3; i++){
- if (_tempArray[i] is Array){
- return merge1(_tempArray);
- break;
- } else {
- m++;
- if (m == _tempArray.length){
- return _tempArray;
- }
- }
- }
- }
- }
- }
热门文章推荐
- [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示例
请稍候...