有关数组的一些处理

                                               Flash AS3.0关于数组方面的添加,删除,查找,字符串等介绍

向数组尾端添加元素     

var array:Array = new Array();    

array.push("a", "b"); 

  //向数组尾端添加单一元素也可以这样:   

  array[array.length] = "c";     

//如果以索引设定的元素不存在,数组本身会自动扩展以包含足够的元素数目.介于中间的元素会被设为undefined:    

  array[5] = "e";      trace(array[4]); //输出: undefined 

向数组开端添加元   

   var array:Array = ["a", "b"];    

  array.unshift("c", "d");   

   trace(array); //输出: c,d,a,b 

删除数组中第一个元素并返回该元素,使用shift()方法     

var letters:Array = new Array("a", "b", "c");    

  var firstLetter:String = letters.shift();   

   trace(letters); //输出: b,c   

   trace(firstLetter); //输出: a 

删除数组中最后一个元素并返回该元素的值,使用pop()方法     

var letters:Array = new Array("a", "b", "c");    

  trace(letters); //输出: a,b,c   

   var letter:String = letters.pop();   

   trace(letters); //输出: a,b   

   trace(letter); //输出: c

  删除数组中的元素,给数组添加新元素并返回删除的元素,使用splice()方法  

splice(startIndex:int, deleteCount:uint, ... values):Array

 startIndex: 一个整数,它指定数组中开始进行插入或删除的位置处的元素的索引;

 deleteCount: 一个整数,它指定要删除的元素数量;

 ... values: 用逗号分隔的一个或多个值的可选列表或数组,此列表或数组将插入到此数组中由 startIndex 参数指定的位置.  

查找数组中第一个相匹配的元素     

var array:Array = ["a", "b", "c", "d", "a", "b", "c", "d"];    

  var match:String = "b";   

   for(var i:int = 0; i < array.length; i++)

       {        

           if(array[i] == match)

                 {     

                       trace("Element with index " + i + " found to match " + match); 

                       //输出: Element with index 1 found to match b   

                       break;      

                }   

      } 

查找数组中最后一个相匹配的元素   

   var array:Array = ["a", "b", "c", "d", "a", "b", "c", "d"]; 

    var match:String = "b";    

  for(var i:int = array.length - 1; i >= 0; i--)

{       

   if(array[i] == match)

       {    

          trace("Element with index " + i + " found to match " + match);    

          //输出: Element with index 5 found to match b    

          break;      

         }   

 } 

把字符串转成数组  使用String.split()方法:

  var list:String = "I am YoungBoy.";   

  var words:Array = list.split(" "); //以空格作为分隔符将字符串切割   

  trace(words); //输出: I,am,YoungBoy. 

把数组转成字符串  使用String.join()方法:     

var myArr:Array = new Array("one", "two", "three");   

var myStr:String = myArr.join(" and ");     

trace(myArr); //输出: one,two,three    

trace(myStr); //输出: one and two and three 

使用对象数组处理相关数据  

    var cars:Array = new Array(); 

    cars.push({make:"Mike", year:1997, color:"blue"}); 

    cars.push({make:"Kelly", year:1986, color:"red"}); 

    for(var i:int = 0; i < cars.length; i++)

    {       

          trace(cars[i].make + " - " + cars[i].year + " - " + cars[i].color);  

   }    

   //输出: 

  //     Mike - 1997 - blue   

  //     Kelly - 1986 - red 

在数组中获取最小或最大值    

   var scores:Array = [10, 4, 15, 8];   

   scores.sort(Array.NUMERIC);   

   trace("Minimum: " + scores[0]); 

  trace("Maximum: " + scores[scores.length - 1]); 

使用for ... in语句读取关联数组元素

 var myObject:Object = new Object(); 

myObject.name = "YoungBoy"; 

myObject.age = 20; 

for(var i:String in myObject)

   {     

    trace(i + ": " + myObject[i]); 

  } 

  //输出: name: YoungBoy

  // age: 20

随机抽取不重复的随机数

var array:Array=new Array();

for (var i:int=0; i<10; i++)

     {  

       array.push(i);

     }

var myarray:Array=array.concat();

for (var j:int=0; j<10; j++)

{  

     trace(myarray.length);

     var k:int=Math.random()*(myarray.length-1);  

     trace("选取图片的编码是"+myarray[k]);

     myarray.splice(k,1);

     trace(myarray);

}

注意: for ... in循环不会显示对象所有的内建属性.例如,循环会显示执行期间新增的特殊属性,但是,不会列出内建对象的方法,即使都是储存在对象属性内.

我就是我,颜色不一样的烟火。 ----辉。
原文地址:https://www.cnblogs.com/klh5211314/p/2722873.html