·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3合并一维数组和二维数组

时间:2014-01-03 11:05cuplayer.com
[AS3]as3合并一维数组和二维数组,比较简单,自己先存着,以免以后用来

合并一维数组和二维数组,哈哈.比较简单,自己先存着,以免以后用来
如:

  1. var a1:Array=[1,2,3,4] 
  2. var a2:Array=[432,4,5,7] 
  3. var a3:Array=[36] 
  4. var aa:Array=[a1,a2,a3] 
  5. var m:mergeArray=new mergeArray; 
  6. //var a:Array=m.Merge(aa);//-----------1 
  7. var a:Array=m.Merge(a1,a2,a3)//--------2 
  8. trace(a + " = " + a.length); 
  9. //1,2都输入1,2,3,4,432,4,5,7,36 = 9  
  1. package 
  2.      
  3.     /** 
  4.      * ...合并多个数组,可合并一维数组和二维数组 
  5.      * @author zkl CuPlayer.com
  6.      */ 
  7.     public class mergeArray 
  8.     { 
  9.         private var i:int; 
  10.          
  11.         private var j:int; 
  12.          
  13.         public function Merge(...args):Array 
  14.         { 
  15.             var _newArray:Array = new Array; 
  16.              
  17.             var len:int = args.length; 
  18.              
  19.             if (len == 1) { 
  20.                  
  21.                 //二维数组 
  22.                 if (args[0] is Array) { 
  23.                      
  24.                     for (i = 0; i < args[0].length; i++ ) { 
  25.                         //合并一维数组 
  26.                          _newArray = mergeing(args[0]);  
  27.                           
  28.                     } 
  29.                 } 
  30.                  
  31.             }else { 
  32.                  
  33.                 _newArray = mergeing(args); 
  34.                  
  35.             } 
  36.                  
  37.             return _newArray; 
  38.              
  39.         } 
  40.          
  41.         /** 
  42.          * 合并一维数组 
  43.          */ 
  44.         private function mergeing(a1:Array):Array 
  45.         { 
  46.             var a:Array = new Array; 
  47.              
  48.             for (i = 0; i < a1.length; i++ ) { 
  49.                      
  50.                     if (a1[i] is Array) { 
  51.                          
  52.                         for (j = 0; j < a1[i].length; j++ ) { 
  53.                           
  54.                             a.push(a1[i][j]); 
  55.                           
  56.                         } 
  57.                          
  58.                     } 
  59.                 } 
  60.              
  61.             return a; 
  62.         } 
  63.          
  64.     } 
  65.      

升级版,不知还有没有更好的办法?

  1. package { 
  2.  
  3.     /** 
  4.      * ...合并多个数组,可合并一维数组和二维数组 
  5.      * @author zkl CuPlayer.com
  6.      * 2010-8-30 14:27 
  7.      */ 
  8.     public class mergeArrayOk { 
  9.         public function Merge(... args):Array { 
  10.             return merge1(args); 
  11.         } 
  12.  
  13.         private function merge1(arr:Array):Array { 
  14.             var i:int, j:int; 
  15.             var _tempArray:Array = new Array; 
  16.             //为了不让每次循环都计算数组长度 
  17.             var len:int = arr.length; 
  18.             for (i = 0; i < len; i++){ 
  19.                 if (arr[i] is Array){ 
  20.                     var len2:int = arr[i].length; 
  21.                     for (j = 0; j < len2; j++){ 
  22.                         if (arr[i][j] != null){ 
  23.                             _tempArray.push(arr[i][j]); 
  24.                         } 
  25.                     } 
  26.                 } else if (arr[i] != null){ 
  27.                     _tempArray.push(arr[i]); 
  28.                 } 
  29.             } 
  30.             //查询_tempArray是否还有数组,有则再次转换 
  31.             var m:int; 
  32.             var len3:int = _tempArray.length; 
  33.             for (i = 0; i < len3; i++){ 
  34.                 if (_tempArray[i] is Array){ 
  35.                     return merge1(_tempArray); 
  36.                     break; 
  37.                 } else { 
  38.                     m++; 
  39.                     if (m == _tempArray.length){ 
  40.                         return _tempArray; 
  41.                     } 
  42.                 } 
  43.             } 
  44.         } 
  45.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器