• JavaScript实现的ArrayList类(转)


      1 /**********************************************************
      2 * JavaScript实现的ArrayList类 
      3 * 
      4 * @author {yangl}
      5 * @version $Revision: 0.5 $ $Date: 2008/04/02 15:00:00 $
      6 * @description
      7 * Method:
      8 * add(element);
      9 * addElementAt(index, element);
     10 * contains(element);
     11 * get(index);
     12 * isEmpty(index);
     13 * indexOf(element);
     14 * lastIndexOf(element);
     15 * remove()
     16 * setElementAt(index, element);
     17 * size();
     18 * toString();
     19 * @example
     20 * var arrList = new ArrayList();
     21 * //var arrList = new ArrayList(10);
     22 * arrList.add("000");
     23 * arrList.add("001");
     24 * arrList.add("002");
     25 *
     26 *********************************************************/
     27 // JavaScript ArrayList
     28 /**//**
     29 Method:
     30         add(element);
     31         addElementAt(index, element);
     32         contains(element);
     33         get(index);
     34         isEmpty(index);
     35         indexOf(element);
     36         lastIndexOf(element);
     37         remove(index);
     38         setElementAt(index, element);
     39         size();
     40         toString();
     41 */
     42 /**//**
     43 Example:
     44         var arrList = new ArrayList();
     45         //var arrList = new ArrayList(10);
     46         arrList.add("000");
     47         arrList.add("001");
     48         arrList.add("002");
     49 */
     50 var ArrayList = function() {
     51     var args = ArrayList.arguments;
     52     var initialCapacity = 10;
     53 
     54     if (args != null && args.length > 0) {
     55         initialCapacity = args[0];
     56     }
     57 
     58     var elementData = new Array(initialCapacity);
     59     var elementCount = 0;
     60 
     61     this.size = function() {
     62         return elementCount;
     63     };
     64 
     65     this.add = function(element) {
     66         //alert("add");
     67         ensureCapacity(elementCount + 1);
     68         elementData[elementCount++] = element;
     69         return true;
     70     };
     71 
     72     this.addElementAt = function(index, element) {
     73         //alert("addElementAt");
     74         if (index > elementCount || index < 0) {
     75             alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);
     76             return;
     77             //throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
     78         }
     79         ensureCapacity(elementCount + 1);
     80         for (var i = elementCount + 1; i > index; i--) {
     81             elementData[i] = elementData[i - 1];
     82         }
     83         elementData[index] = element;
     84         elementCount++;
     85     };
     86 
     87     this.setElementAt = function(index, element) {
     88         //alert("setElementAt");
     89         if (index > elementCount || index < 0) {
     90             alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);
     91             return;
     92             //throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
     93         }
     94         elementData[index] = element;
     95     };
     96 
     97     this.toString = function() {
     98         //alert("toString()");
     99         var str = "{";
    100         for (var i = 0; i < elementCount; i++) {
    101             if (i > 0) {
    102                 str += ",";
    103             }
    104             str += elementData[i];
    105         }
    106         str += "}";
    107         return str;
    108     };
    109 
    110     this.get = function(index) {
    111         //alert("elementAt");
    112         if (index >= elementCount) {
    113             alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);
    114             return;
    115             //throw ( new Error( -1,"ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount ) );
    116         }
    117         return elementData[index];
    118     };
    119 
    120     this.remove = function(index) {
    121         if (index >= elementCount) {
    122             alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);
    123             //return;
    124             throw (new Error(-1, "ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount));
    125         }
    126         var oldData = elementData[index];
    127         for (var i = index; i < elementCount - 1; i++) {
    128             elementData[i] = elementData[i + 1];
    129         }
    130         elementData[elementCount - 1] = null;
    131         elementCount--;
    132         return oldData;
    133     };
    134 
    135     this.isEmpty = function() {
    136         return elementCount == 0;
    137     };
    138 
    139     this.indexOf = function(elem) {
    140         //alert("indexOf");
    141         for (var i = 0; i < elementCount; i++) {
    142             if (elementData[i] == elem) {
    143                 return i;
    144             }
    145         }
    146         return -1;
    147     };
    148 
    149     this.lastIndexOf = function(elem) {
    150         for (var i = elementCount - 1; i >= 0; i--) {
    151             if (elementData[i] == elem) {
    152                 return i;
    153             }
    154         }
    155         return -1;
    156     };
    157 
    158     this.contains = function(elem) {
    159         return this.indexOf(elem) >= 0;
    160     };
    161 
    162     function ensureCapacity(minCapacity) {
    163         var oldCapacity = elementData.length;
    164         if (minCapacity > oldCapacity) {
    165             var oldData = elementData;
    166             var newCapacity = parseInt((oldCapacity * 3) / 2 + 1);
    167             if (newCapacity < minCapacity) {
    168                 newCapacity = minCapacity;
    169             }
    170             elementData = new Array(newCapacity);
    171             for (var i = 0; i < oldCapacity; i++) {
    172                 elementData[i] = oldData[i];
    173             }
    174         }
    175     }
    176 };
  • 相关阅读:
    datagridview 数据刷新 从新绑定
    SVN 出现This client is too old to work with working copy...错误
    【转】ASPX和HTML获取系统根目录的路径
    C#程序最小化到托盘图标
    Winform的html编辑控件htmleditor 有时候无法为里面HTML属性初始化
    C# HttpWebRequest保存cookies模拟登录的方法【z】【解决】
    快速生成解决方案 ctrl + shilf + b
    datagridview只允许单选
    数据库int类型 才可以自增长
    C# dataTable实用例
  • 原文地址:https://www.cnblogs.com/zhangwei595806165/p/2744320.html
Copyright © 2020-2023  润新知