• Flex Array 操作


    Array提供了多种方法插入和删除元素。通过这些方法,可以快速按需要操作Array中的元素。Array类提供了一对方法push和pop方法,使得Array类实现了栈的结构。push方法可以将元素插入到Array的尾部。pop方法删除Array最尾部的元素。这样就实现了一个先进后出的栈结构。实现代码如下所示。

    var flexArray:Array=new Array();

    flexArray.push(“Adobe”);                                                                    //添加元素“Adobe”到Array的尾部

    trace(flexArray);                                                                                    //输出“Adobe”

    flexArray.push(“Macromedia”);                                                          //添加元素“Macromedia”到Array的尾部

    trace(flexArray);                                                                                    //输出“Adobe,Macromeida”

    flexArray.push(“Flex”);                                                                         //添加元素“Flex”到Array的尾部

    trace(flexArray);                                                                                    //输出“Adobe,Macromeida,Flex”

    flexArray.pop();                                                                                     //删除Array尾部元素

    trace(flexArray);                                                                                    //输出“Adobe,Macromeida”

    flexArray.pop();                                                                                     //删除Array尾部元素

    trace(flexArray);                                                                                    //输出“Adobe”

    flexArray.pop();                                                                                     //删除Array尾部元素

    trace(flexArray);                                                                                     //输出“”

    由上面的代码可以看出,通过push和pop方法可以用一个Array类的一个实例构建一个栈的数据结构。通过这样的数据结构,可以实现更为复杂的算法。

    Array类也提供了一组shift和unshift的方法向Array的实例添加元素和删除元素。与push与pop不同,unshift添加方式是在Array实例的头部添加元素。而shift方法是从Array类实例的头部开始删除元素。如下代码所示。

    var flexArray:Array=new Array();

    flexArray. unshift (“Adobe”);                                                               //添加元素“Adobe”到Array的头部

    trace(flexArray);                                                                                    //输出“Adobe”

    flexArray. unshift (“Macromedia”);                                                     //添加元素“Macromedia”到Array的头部

    trace(flexArray);                                                                                    //输出“Macromeida, Adobe”

    flexArray. unshift (“Flex”);                                                                    //添加元素“Flex”到Array的头部

    trace(flexArray);                                                                                    //输出“Flex, Macromeida, Adobe”

    flexArray. shift ();                                                                                   //删除Array头部元素

    trace(flexArray);                                                                                    //输出“Macromeida, Adobe”

    flexArray. shift ();                                                                                   //删除Array头部元素

    trace(flexArray);                                                                                    //输出“Adobe”

    flexArray. shift ();                                                                                   //删除Array头部元素

    trace(flexArray);                                                                                    //输出“”

    除了上面提到的这几种顺序的插入和删除Array元素的方法外,Array类还提供了一个方法splice用来通过指定位置添加或者删除元素。splice方法使用的方式如下代码所示。

    var flexArray:Array=[“Adobe”,”Flex”];

    flexArray.splice(1,0,”Maromedia”);     //删除从索引位置1开始的0个元素,添加”Maromedia”到实例中

    trace(flexArray);                                      //输出“Adobe,Macromeida,Flex”

    flexArray.splice(1,1);                             //删除从索引位置1开始的1个元素

    trace(flexArray);                                      //输出“Adobe,Flex”

    flexArray.splice(1,1,”Flex3”);                //删除从索引位置1开始的1个元素,添加” Flex3”到实例中

    trace(flexArray);                                      //输出“Adobe,Flex3”

    splice方法有两个整型参数。第一个整型参数用来指定操作元素的索引位置。第二个整型参数指定删除元素的个数,如果为0那么是不删除元素的。如果存在第三个参数,则表示进行完删除后插入的元素。这样通过splice就可以一次完成指定位置先删除后插入的替换操作。

    在使用Array类实例的过程中可以通过delete关键字进行删除操作。通过delete关键字进行删除时不改变Array的长度。访问被删除元素返回undefined类型。使用方法如下所示。

    var flexArray:Array=[“Adobe”,”Macromedia”,”Flex”];

    delete(flexArray[1]);                                                  //删除索引位置为1的元素

    trace(flexArray);                                                        //输出““Adobe,,Flex””

    trace(flexArray[1]);                                                    //输出“undefined”

    trace(flexArray.length);                                            //输出3

    减小Array实例的长度定义也会删除新长度定义之外的元素。如下代码所示。

    var flexArray=[“Adobe”,”Macromedia”,”Flex”];

    flexArray.length=2;                                                   //改变Array长度的定义

    trace(flexArray);                                                        //输出“Adobe,Macromedia”

  • 相关阅读:
    LeetCode题解之Leaf-Similar Trees
    LeetCode题解之 Increasing Order Search Tree
    建立git仓库的步骤
    Jquery-1(基础)
    JavaScript-12(脚本化CSS)
    JavaScript-11(DOM文档对象模型)
    JavaScript-10(JavaScript事件)
    JavaScript-实现下拉菜单
    JavaScript-9(BOM浏览器对象模型)
    JavaScript-字符与编码转换
  • 原文地址:https://www.cnblogs.com/anfeind/p/1489371.html
Copyright © 2020-2023  润新知