创建数组

Ø  无参数:var aEmployees:Array = new Array();

Ø  指定元素数量的一个单独的参数:var aEmployees:Array = new Array(4);

Ø  参数列表,每一个参数都是要插入数组的一个新元素的新值:

var aEmployees:Array = new Array(“Arun”,”Peter”,”Chris”,”Heather”);

 

var aEmployees:Array = [“Arun”,”Peter”,”Chris”,”Heather”];

var aEmployees:Array = [];

 

给数组添加值

Ø  用数组访问符号进行赋值。

arrayName[index] = value ;

Ø  在数组的末尾追加新的元素

aEmployees.push(“Ruth”,”Hao”,”Laura”);

Ø  在数组的开始处追加新的元素

aEmployees.unshift(“Ruth”,”Hao”,”Laura”);

Ø  在数组中插入元素

arrayName.splice(startingIndex, numberOfElementsToDelete,element1[, … elementN]);

举例子:

Var aEmployees:Array = [“Arun”,”Peter”,”Chris”,”Heather”];

aEmployees.splice(3,0,”Ruth”,”Hao”,”Laura”);

trace(aEmployees.toString());

输出:Arun,Peter,Chris,Ruth,Hao,Laura,Heather.

 

从数组中删除元素

Ø  删除数组的最后一个元素

var sAnEmployee:String = String(aEmployees.pop());

Ø  删除数组的第一个元素

var sAnEmployee:String = String(aEmployees.shift());

Ø  删除数组中间的元素

var aRemovedEmployees:Array = aEmployees.splice(2,2);

从数组中删除元素

Ø  使用一维数组

Ø  使用并行数组(多个一维数组)

Ø  使用多维数组

var aEmployees:Array = new Array();

aEmployees.push([“Arun”,”555-1234”]);

aEmployees.push([“Peter”,”555-4321”]);

aEmployees.push([“Chris”,”555-5678”]);

aEmployees.push([“Heather”,”555-8765”]);

for(var i:Number = 0; i<aEmployees.length; i++){

         trace(“Employee:”+aEmployees[i][0]);

         trace(“Phone Number:”+aEmployees[i][1]);

}

Ø  使用对象数组

var aEmployees:Array = new Array();

aEmployees.push({employee:”Arun”,phone:”555-1234”});

aEmployees.push({employee:”Peter”,phone:”555-4321”});

aEmployees.push({employee:”Chris”,phone:”555-5678”});

for(var i:Number = 0; i<aEmployees.length;i++){

         trace(“Employee:”+aEmployees[i].employee);

         trace(“Phone Number:”+aEmployees[i].phone);

}

将数组转换成列表

Ø  数组-----à列表

var aEmployees:Array = [“Arun”,”Peter”,”Chris”,”Heather”];

var sEmployees:String = aEmployees.join(“;”);

trace(sEmployees);

 

输出面板将显示如下内容

Arun;Peter;Chris;Heather

Ø  列表-----à数组

var sEmployees:String = “Arun,Peter,Chris,Heather”;

var aEmployees:Array = sEmployees.split(“,”);

从存在的数组创建新数组

Ø  连接数组

var aEmployeesExec:Array = [“Arun”,”Peter”,”Chris”,”Heather”];

var aEmployeesNew:Array = [“Gilberto”,”Mary”];

var aEmployeesStaff:Array = [“Ayla”,”Riad”];

var aEmployeesAll:Array = aEmployeesExec.concat(aEmployeesNew,aEmployeesStaff);

Ø  提取数组元素的子集

var aEmployeesAll:Array = [“Arun”,”Peter”,”Chris”,”Heather”,”Gilberto”,”Mary”,”Ayla”,”Riad”];

var aEmployeesExec:Array = aEmployeesAll.slice(0,4);   //包含4个元素

var aEmployeesNew:Array = aEmployeesAll.slice(4,6);

var aEmployeesStaff:Array = aEmployeesAll.slice(6);

对数组进行排序

Ø  简单的排序

为了实现按字母顺序排序,只需要简单地创建数组并不带参数调用sort()方法。

var aEmployees:Array = [“Arun”,”Peter”,”Chris”,”Heather”];

aEmployees.sort();

Ø  较复杂的排序

²  按数字进行排序

var aNumbers:Array = [10,1,2,15,21,13,33,3];

aNumbers.sort(Array.NUMERIC);

²  按降序进行排序

aEmployees.sort(Array.DESCENDING);

²  不区分大小写进行排序

aWords.sort(Array.CASEINSENSITIVE);

²  排序并测试唯一值

aEmployees.sort(Array.UNIQUESORT);

如果数组的元素都是惟一的,该数组就按字母顺序进行升序排列。

如果有一个或多个重复的元素,sort()方法就返回0.并且该数组不被排序。

²  获取排序后的索引

返回一个表示排序后元素的索引的数组。这个选项不会对原来的数组进行排序,但提供了一个按排序后的顺序访问元素的方法。原来的数组不会被修改。

var aEmployees:Array = [“Arun”,”Peter”,”Chris”,”Heather”];

var aSortedIndices:Array = aEmployees.sort(Array.RETURNINDEXEDARRAY);

trace(aEmployees.toString());

trace(aSortedIndices.toString());

for(var i:Number = 0;i< aSortedIndices.length;i++){

         trace(aEmployees[aSortedIndices[i]]);

}

²  用多个标志进行排序

aWords.sort(Array.DESCENDING|Array.CASEINSENSITIVE);

²  用自定义算法进行排序

例子:该函数首先按值得类型(城市的名字或国家的名字)然后按字母顺序对数组的值进行排序

         function isInArray(sElement:String, aArray:Array){

                   for(var i:Number = 0; I < aArray.length; i++){

                            if(sElement == aArray[i]){

                                     return true;

}

}

return false;

}

function sorter(a:String , b:String):Number{

         var aCountries:Array =[“Mexico”,”Vietnam”,”Japan”];

         var aCities:Array = [“Caracas”,”Paris”,”Berlin”];

         if((isInArray(a,aCountries) && isInArray(b,aCities)) || (isInArray(b,aCountries) && isInArray(a,aCities))){

                   return 1;

}

If(a.toUpperCase() > b.toUpperCase()){

         return 1;

}

else if(a.toUpperCase() < b.toUpperCase()){

         return -1;

}

else{

         return 0;

}

}

var aPlaces:Array = [“Berlin”,”Vietnam”,”Japan”,”Caracas”,”Mexico”,”Paris”];

aPlaces.sort(sorter);

trace(aPlaces.toString());

Ø  对关联数组的数组进行排序

²  按一个单独的键进行排序

function displayArray(aArray:Array){

         var sElement:String = null;

         for(var i:Number = 0; I < aArray.length; i++){

                   sElement = “” ;

                   for(var key in aArray[i]){

                            sElement += aArray[i][key] + “  ”;

}

trace(sElement);

}

}

 

var aCars:Array = new Array();

aCars.push({make:”Oldsmobile”,model:”Alero”,extColor:”blue”});

aCars.push({make:”Honda”,model:”Accord”,extColor:”red”});

aCars.push({make:”Volvo”,model:”242”,extColor:”red”});

aCars.sortOn(“make”);

displayArray(aCars);

²  按多个键进行排序

aCars.sortOn([“make”,”model”,”extColor”]);

²  用排序标志进行排序

aCars.sortOn([“make”,”model”,”extColor”],Array.DESCENDING);

²  倒转数组

aEmployees.reverse();

 

posted on 2011-07-05 23:05  pandy  阅读(228)  评论(0编辑  收藏  举报