数组

来源:互联网 发布:js二级下拉菜单特效 编辑:程序博客网 时间:2024/06/06 01:14

须要搞明白的问题:

 

1、脚本中的数组(数组是一个类)定义和使用?
 
(1)常用的方式
        定义时指明大小 或 定义时候赋值给出内容

        例、

1、

var myArray:Array = ["one", "two", "three"];
2、
<mx:Array>
    <mx:String>AK</mx:String>
    <mx:String>AL</mx:String>
    <mx:String>AR</mx:String>
</mx:Array>
3、
<mx:Script><![CDATA[
    var expenses:Array = [
        {Month: "January", Profit: 2000, Expenses: 1500, Amount: 450},
        {Month: "February", Profit: 1000, Expenses: 200, Amount: 600},
        {Month: "March", Profit: 1500, Expenses: 500, Amount: 300},
        {Month: "April", Profit: 500, Expenses: 300, Amount: 500},
        {Month: "May", Profit: 1000, Expenses: 450, Amount: 250},
        {Month: "June", Profit: 2000, Expenses: 500, Amount: 700}
    ];
]]></mx:Script>
(2)采用类的方法进行赋值(待解决)


2、FLEX 数组的定义和使用?

var planets:Array = new Array();
planets.push("Mars"); // 数组内容:Mars
planets.unshift("Mercury"); // 数组内容:Mercury,Mars
planets.splice(1, 0, "Venus", "Earth");

//splice() 的第一个参数是整数 1,它用于指
示从索引 1 处开始插入。传递给 splice() 的第二个参数是整数 0,它表示不应删除任何项。//
trace(planets); // 数组内容:Mercury,Venus,Earth,Mars

3、数组的遍厉?

(1)、直接显示:

var names:Array = new Array("John", "Jane", "David");
trace(names.length); // output: 3
trace(names[0]); // output: John
trace(names[1]); // output: Jane
trace(names[2]); // output: David

(2)、用循环:

var poets:Array = new Array();
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});

poets.sortOn("born", Array.NUMERIC);
for (var i:int = 0; i < poets.length; ++i) {
    trace(poets[i].name, poets[i].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/