欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

自己封裝js的ArrayList類

系統 1954 0

眾所周之,js是沒有ArrayList類的,但是js自帶了Array類(雖然在js中已經是動態數組了),不過Array類使用起來還是挺別扭的,尤其是一些方法名稱,更是讓人摸不著頭腦,于是就有了自己封裝一個ArrayList類的想法。

?

?

      (
      
        function
      
      
        (win) {
    
      
      
        var
      
       ArrayList = 
      
        function
      
      
        () {
        
      
      
        this
      
      .datas =
      
         [];
    };

    
      
      
        var
      
       proto =
      
         ArrayList.prototype;

    proto.size 
      
      = 
      
        function
      
      
        () {
        
      
      
        return
      
      
        this
      
      
        .datas.length;
    };

    proto.isEmpty 
      
      = 
      
        function
      
      
        () {
        
      
      
        return
      
      
        this
      
      .size() === 0
      
        ;
    };

    proto.contains 
      
      = 
      
        function
      
      
        (value) {
        
      
      
        return
      
      
        this
      
      .datas.indexOf(value) !== -1
      
        ;
    };

    proto.indexOf 
      
      = 
      
        function
      
      
        (value) {
        
      
      
        for
      
       ( 
      
        var
      
       index 
      
        in
      
      
        this
      
      
        .datas) {
            
      
      
        if
      
       (
      
        this
      
      .datas[index] ===
      
         value) {
                
      
      
        return
      
      
         index;
            }
        }

        
      
      
        return
      
       -1
      
        ;
    };

    proto.lastIndexOf 
      
      = 
      
        function
      
      
        (value) {
        
      
      
        for
      
       ( 
      
        var
      
       index = 
      
        this
      
      .size(); index >= 0; index--
      
        ) {
            
      
      
        if
      
       (
      
        this
      
      .datas[index] ===
      
         value) {
                
      
      
        return
      
      
         index;
            }
        }
    };

    proto.toArray 
      
      = 
      
        function
      
      
        () {
        
      
      
        return
      
      
        this
      
      
        .datas;
    };

    proto.outOfBound 
      
      = 
      
        function
      
      
        (index) {
        
      
      
        return
      
       index < 0 || index > (
      
        this
      
      .size() - 1
      
        );
    };

    proto.get 
      
      = 
      
        function
      
      
        (index) {
        
      
      
        if
      
       (
      
        this
      
      
        .outOfBound(index)) {
            
      
      
        return
      
      
        null
      
      
        ;
        }

        
      
      
        return
      
      
        this
      
      
        .datas[index];
    };

    proto.set 
      
      = 
      
        function
      
      
        (index, value) {
        
      
      
        this
      
      .datas[index] =
      
         value;
    };

    proto.add 
      
      = 
      
        function
      
      
        (value) {
        
      
      
        this
      
      
        .datas.push(value);
    };

    proto.insert 
      
      = 
      
        function
      
      
        (index, value) {
        
      
      
        if
      
       (
      
        this
      
      
        .outOfBound(index)) {
            
      
      
        return
      
      
        ;
        }

        
      
      
        this
      
      .datas.splice(index, 0
      
        , value);
    };

    proto.remove 
      
      = 
      
        function
      
      
        (index) {
        
      
      
        if
      
       (
      
        this
      
      
        .outOfBound(index)) {
            
      
      
        return
      
      
        false
      
      
        ;
        }

        
      
      
        this
      
      .datas.splice(index, 1
      
        );
        
      
      
        return
      
      
        true
      
      
        ;
    };

    proto.removeValue 
      
      = 
      
        function
      
      
        (value) {
        
      
      
        if
      
       (
      
        this
      
      
        .contains(value)) {
            
      
      
        this
      
      .remove(
      
        this
      
      
        .indexOf(value));
            
      
      
        return
      
      
        true
      
      
        ;
        }
        
      
      
        return
      
      
        false
      
      
        ;
    };

    proto.clear 
      
      = 
      
        function
      
      
        () {
        
      
      
        this
      
      .datas.splice(0, 
      
        this
      
      
        .size());
    };

    proto.addAll 
      
      = 
      
        function
      
      
        (list) {
        
      
      
        if
      
       (!list 
      
        instanceof
      
      
         ArrayList) {
            
      
      
        return
      
      
        false
      
      
        ;
        }

        
      
      
        for
      
       ( 
      
        var
      
       index 
      
        in
      
      
         list.datas) {
            
      
      
        this
      
      
        .add(list.get(index));
        }

        
      
      
        return
      
      
        true
      
      
        ;
    };

    proto.insertAll 
      
      = 
      
        function
      
      
        (index, list) {
        
      
      
        if
      
       (
      
        this
      
      
        .outOfBound(index)) {
            
      
      
        return
      
      
        false
      
      
        ;
        }

        
      
      
        if
      
       (!list 
      
        instanceof
      
      
         ArrayList) {
            
      
      
        return
      
      
        false
      
      
        ;
        }
        
        
      
      
        var
      
       pos =
      
         index;
        
      
      
        for
      
      (
      
        var
      
       index 
      
        in
      
      
         list.datas)
        {
            
      
      
        this
      
      .insert(pos++
      
        , list.get(index));
        }
        
      
      
        return
      
      
        true
      
      
        ;
    };
    
    
      
      
        function
      
      
         numberorder(a, b) { 
        
      
      
        return
      
       a -
      
         b; 
    }
    
    proto.sort 
      
      = 
      
        function
      
      
        (isNumber){
        
      
      
        if
      
      
        (isNumber){
            
      
      
        this
      
      
        .datas.sort(numberorder);
            
      
      
        return
      
      
        ;
        }
        
        
      
      
        this
      
      
        .datas.sort();
    };
    
    proto.toString 
      
      = 
      
        function
      
      
        (){
        
      
      
        return
      
       "[" + 
      
        this
      
      .datas.join() + "]"
      
        ;
    };
    
    proto.valueOf 
      
      = 
      
        function
      
      
        (){
        
      
      
        return
      
      
        this
      
      
        .toString();
    };

    win.ArrayList 
      
      =
      
         ArrayList;
})(window);






      
    

我們寫一個頁面測試一下。

        
          <!
        
        
          DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
        
        
          >
        
        
          <
        
        
          html
        
        
          >
        
        
          <
        
        
          head
        
        
          >
        
        
          <
        
        
          meta 
        
        
          http-equiv
        
        
          ="Content-Type"
        
        
           content
        
        
          ="text/html; charset=UTF-8"
        
        
          >
        
        
          <
        
        
          title
        
        
          >
        
        Insert title here
        
          </
        
        
          title
        
        
          >
        
        
          <
        
        
          script 
        
        
          type
        
        
          ="text/javascript"
        
        
           src
        
        
          ="ArrayList.js"
        
        
          ></
        
        
          script
        
        
          >
        
        
          <
        
        
          script 
        
        
          type
        
        
          ="text/javascript"
        
        
          >
        
        
          
window.print 
        
        
          =
        
        
          function
        
        
          (value) {
    document.write(value);
};

window.println 
        
        
          =
        
        
          function
        
        
          (value) {
    print(value);
    document.write(
        
        
          "
        
        
          <br/>
        
        
          "
        
        
          );
};

window.onload 
        
        
          =
        
        
          function
        
        
          ()
{
    
        
        
          var
        
        
           list 
        
        
          =
        
        
          new
        
        
           ArrayList();
    list.add(
        
        
          "
        
        
          jack
        
        
          "
        
        
          );
    list.add(
        
        
          43
        
        
          );
    list.add(
        
        
          true
        
        
          );
    
    println(list.get(
        
        
          0
        
        
          ));
    println(list.get(
        
        
          1
        
        
          ));
    println(list.get(
        
        
          2
        
        
          ));
    println(list.get(
        
        
          3
        
        
          ));
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    println(list.size());
    
    list.remove(
        
        
          2
        
        
          );
    println(list);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    println(list.isEmpty());
    list.clear();
    println(list.isEmpty());
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    list.add(
        
        
          "
        
        
          jack
        
        
          "
        
        
          );
    list.add(
        
        
          43
        
        
          );
    list.add(
        
        
          true
        
        
          );
    
        
        
          var
        
        
           list2 
        
        
          =
        
        
          new
        
        
           ArrayList();
    list2.addAll(list);
    println(list2);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    list2.insert(
        
        
          1
        
        
          ,
        
        
          "
        
        
          male
        
        
          "
        
        
          );
    println(list2);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    list2.removeValue(
        
        
          true
        
        
          );
    println(list2);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    list2.insertAll(
        
        
          2
        
        
          ,list);
    println(list2);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    println(list2.contains(
        
        
          "
        
        
          jack
        
        
          "
        
        
          ));
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    list2.clear();
    list2.add(
        
        
          1111
        
        
          );
    list2.add(
        
        
          222
        
        
          );
    list2.add(
        
        
          33
        
        
          );
    list2.add(
        
        
          4
        
        
          );
    
    list2.sort();
        
        
          //
        
        
          按字母順序排
        
        
              println(list2);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
    
    list2.sort(
        
        
          true
        
        
          );
        
        
          //
        
        
          按數字順序排
        
        
              println(list2);
    println(
        
        
          "
        
        
          ----------------------
        
        
          "
        
        
          );
}

        
        
          </
        
        
          script
        
        
          >
        
        
          </
        
        
          head
        
        
          >
        
        
          <
        
        
          body
        
        
          >
        
        
          </
        
        
          body
        
        
          >
        
        
          </
        
        
          html
        
        
          >
        
      
      
      
    

輸出結果如下:

        
          jack
43
true
null
----------------------
3
[jack,43]
----------------------
false
true
----------------------
[jack,43,true]
----------------------
[jack,male,43,true]
----------------------
[jack,male,43]
----------------------
[jack,male,jack,43,true,43]
----------------------
true
----------------------
[1111,222,33,4]
----------------------
[4,33,222,1111]
----------------------
        
      

自己封裝js的ArrayList類


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩精品久久一区二区三区 | A片扒开双腿猛进入免费观看 | 日韩一区免费在线观看 | 中国大陆高清aⅴ毛片 | 欧美高清成人 | 狠狠色噜噜狠狠狠狠米奇7777 | 国产成人一区二区三区 | 久久一日本道色综合久久 | 天天拍久久 | 亚洲免费视频网 | 96自拍视频 | 国产成人无码区免费内射一片色欲 | 天天拍拍夜夜出水 | 亚洲国产中文字幕 | 一个看片免费视频www | 国产乱码精品一区二区三区中文 | 视频一区在线观看 | 免费观看欧美一级片 | 轻轻啪在线视频播放 | 91精品国产777在线观看 | 中国一级特黄毛片大片 | 婷婷激情五月综合 | 91短视频免费在线观看 | 国产一级毛片视频 | 91伊人 | 国产免费一级淫片 | 羞羞视频网站在线观看 | 999国产视频 | 亚洲成在人线免费视频 | 天天怕夜夜怕狠狠怕 | 日本一本免费一二区 | 精品免费久久久久欧美亚一区 | 日韩一区精品视频 | 午夜影院在线免费观看 | 成人午夜在线 | 台湾佬中文娱乐2222vvv | 性色网址| 日韩在线不卡一区 | 国产小视频精品 | 国产欧美日韩一区 | 艹逼免费视频 |