• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. JavaScript數(shù)組的棧方法與隊列方法

        時間:2024-08-19 09:57:28 JavaScript 我要投稿
        • 相關(guān)推薦

        JavaScript數(shù)組的棧方法與隊列方法

          JavaScript數(shù)組的棧方法與隊列方法

          數(shù)組(Array)和對象(Object)應(yīng)該是JavaScript中使用最多也是最頻繁的兩種類型了,Array提供了很多常用的方法:棧方法、隊列方法、重排序方法、操作方法、位置方法、迭代方法等等。

          1、Array的棧方法

          棧是一種LIFO(Last-In-First-Out,后進先出)的數(shù)據(jù)結(jié)構(gòu),也就是最新添加的項最早被移除。棧中項的插入(push)和移除,只發(fā)生在一個位置——棧的頂部。ECMAScript為數(shù)組提供了push()和pop()方法,可以實現(xiàn)類似棧的行為。下面兩圖分別演示了入棧與出棧操作。

          push()方法可以接收任意數(shù)據(jù)的參數(shù),把它們逐個添加到數(shù)組末尾,并返回修改后的數(shù)組長度。pop()方法從數(shù)組末尾移除最后一項,減少數(shù)組的length值

          var students = [];students.push("bluce","jordan","marlon","kobe");//入棧4項alert(students.length); //4alert(students[0]); //"bluce",第一項在棧的底部alert(students[1]); //"jordan"students.push("paul");alert(students.length); //5var item = students.pop(); //"paul"alert(students.length); //4

          2、Array的隊列方法

          棧數(shù)據(jù)結(jié)構(gòu)的訪問規(guī)則是LIFO(后進先出),而隊列數(shù)據(jù)結(jié)構(gòu)的訪問規(guī)則是FIFO(First-In-First-Out,先進先出)。隊列在列表的末端添加項,從列表的前端移除項。push()方法是向數(shù)組末端添加項的方法,因此要模擬隊列只需一個從數(shù)組前端取得項的方法——shift(),其能夠移除數(shù)組中的第一個項并返回該項,同時數(shù)組的length-1。結(jié)合使用shift()和push()方法,可以像使用隊列一樣使用數(shù)組。

          var students = [];students.push("bluce","jordan","marlon","kobe");//入隊4項//students=["bluce","jordan","marlon","kobe"];alert(students.length); //4alert(students[0]); //"bluce",第一項在棧的底部alert(students[1]); //"jordan"students.push("paul");alert(students.length); //5//students=["bluce","jordan","marlon","kobe","paul"];var item = students.shift(); //"bluce"alert(students.length); //4//students=["jordan","marlon","kobe","paul"];

          此外,ECMAScript還提供了unshift()方法,它能在數(shù)組前端添加任意個項并返回新數(shù)組的長度。因此,結(jié)合使用unshift()和pop()方法,可以從相反的方向來模擬隊列,即在數(shù)組的前端添加項,從數(shù)組末端移除項

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【JavaScript數(shù)組的棧方法與隊列方法】相關(guān)文章:

        JavaScript數(shù)組常用方法介紹09-04

        JavaScript常用方法匯總10-25

        javascript跨域訪問的方法07-09

        javascript編程異常處理的方法08-04

        JavaScript fontcolor方法入門實例07-07

        Java數(shù)組操作方法大全08-22

        使用ajax操作JavaScript對象的方法09-28

        c語言字符數(shù)組使用方法10-14

        詳解JavaScript中的splice()使用方法08-20

        關(guān)于javascript尋找錯誤方法整理05-23

        在线咨询
        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 中文字幕亚洲乱码在线 | 久久9热re这里只有精品6 | 亚洲人成电影在线观看天堂色 | 亚洲精品在线观看按摩不卡 | 亚洲综合日韩精品高清一区 | 亚洲日韩国产综合一区二区三区 |

            JavaScript數(shù)組的棧方法與隊列方法

              JavaScript數(shù)組的棧方法與隊列方法

              數(shù)組(Array)和對象(Object)應(yīng)該是JavaScript中使用最多也是最頻繁的兩種類型了,Array提供了很多常用的方法:棧方法、隊列方法、重排序方法、操作方法、位置方法、迭代方法等等。

              1、Array的棧方法

              棧是一種LIFO(Last-In-First-Out,后進先出)的數(shù)據(jù)結(jié)構(gòu),也就是最新添加的項最早被移除。棧中項的插入(push)和移除,只發(fā)生在一個位置——棧的頂部。ECMAScript為數(shù)組提供了push()和pop()方法,可以實現(xiàn)類似棧的行為。下面兩圖分別演示了入棧與出棧操作。

              push()方法可以接收任意數(shù)據(jù)的參數(shù),把它們逐個添加到數(shù)組末尾,并返回修改后的數(shù)組長度。pop()方法從數(shù)組末尾移除最后一項,減少數(shù)組的length值

              var students = [];students.push("bluce","jordan","marlon","kobe");//入棧4項alert(students.length); //4alert(students[0]); //"bluce",第一項在棧的底部alert(students[1]); //"jordan"students.push("paul");alert(students.length); //5var item = students.pop(); //"paul"alert(students.length); //4

              2、Array的隊列方法

              棧數(shù)據(jù)結(jié)構(gòu)的訪問規(guī)則是LIFO(后進先出),而隊列數(shù)據(jù)結(jié)構(gòu)的訪問規(guī)則是FIFO(First-In-First-Out,先進先出)。隊列在列表的末端添加項,從列表的前端移除項。push()方法是向數(shù)組末端添加項的方法,因此要模擬隊列只需一個從數(shù)組前端取得項的方法——shift(),其能夠移除數(shù)組中的第一個項并返回該項,同時數(shù)組的length-1。結(jié)合使用shift()和push()方法,可以像使用隊列一樣使用數(shù)組。

              var students = [];students.push("bluce","jordan","marlon","kobe");//入隊4項//students=["bluce","jordan","marlon","kobe"];alert(students.length); //4alert(students[0]); //"bluce",第一項在棧的底部alert(students[1]); //"jordan"students.push("paul");alert(students.length); //5//students=["bluce","jordan","marlon","kobe","paul"];var item = students.shift(); //"bluce"alert(students.length); //4//students=["jordan","marlon","kobe","paul"];

              此外,ECMAScript還提供了unshift()方法,它能在數(shù)組前端添加任意個項并返回新數(shù)組的長度。因此,結(jié)合使用unshift()和pop()方法,可以從相反的方向來模擬隊列,即在數(shù)組的前端添加項,從數(shù)組末端移除項