ARRAYLIST class implemented by JavaScript [original]

xiaoxiao2021-03-06  63

/ ************************************************** ********* * JavaScript implemented arraylist class * * @Author yoinn {yoinn@21cn.com} * @version $ revision: 0.5 $ date: 2004/09/28 15:00:00 $ * @Description * method: * add (element); * addelementat; * Contains; * get (index); * iSempty (index); * indexof (element); * LastIndexof (Element); * REMOVE () * SETELEMENTAT (INDEX, ELEMENT); * Size (); * toString (); * @Example * var arrlist = new arraylist (); * // var arrlist = new arraylist (10); * arrlist.add ( "000"); * arrlist.add ("001"); * arrlist.add ("002"); * *********************** ******************************************* / var arraylist = function () {

Var args = arraylist.arguments; var initialcapacity = 10; if (args! = null && args.Length> 0) {InitialCapacity = args [0];}

Var elementdata = new array (initialcapacity); var elementcount = 0;

THIS.SIZE = function () {return ElementCount;}

{// alert ("add"); ENSURECAPACITY (ElementCount 1); ElementData [ElementCount ] = Element; return true;}

this.addelementat = function (index, element) {// alert ("addelementat"); if (INDEX> ElementCount || Index <0) {Alert ("INDExOFboundSexception, INDEX:" Index ", Size:" ElementCount Return; // throw (new error (-1, "indexoutofboundsexception, index:" index ", size:" elementcount));} ENSURECAPACITY (ElementCount 1); for (var i = elementcount 1; i > index; i--) {ElementData [i] = ElementData [i-1]} ElementData [index] = Element; ElementCount ;} this.selection = function (index, element) {// alert ("setElementat"); if (index> elementCount || index <0) {alert ( "IndexOutOfBoundsException, index:" index ", Size:" elementCount); return; // throw (new Error (-1, "IndexOutOfBoundsException, index:" Index ", Size:" ElementCount));} ElementData [index] = Element;

THIS.TOSTRING = function () {// alert ("toString ()"); var str = "{"; for (var i = 0; i 0) {STR = ",";} STR = ElementData [i];} Str = "}"; return str;} this.Get = function (index) {// alert ("elementatat"); if (index> = elementcount) {alert ( "ArrayIndexOutOfBoundsException," index "> =" elementCount); return; // throw (new Error (-1, "ArrayIndexOutOfBoundsException," index "> =" elementCount));} return elementData [ Index];} this.remove = function (index) {if ("ArrayIndexOfficSexception," index "> =" elementcount); // return; throw (new error (-1, "ArrayIndexOfboundsexception," index "> =" elementcount));} var} var}; = = index; EmentCount - 1; i ) {ELEMENTDATA [I] = ElementData [i 1];} ElementData [ElementCount - 1] = null; elementcount--; return olddata;} this.isempty = function () {Return ElementCount == 0 }

THISDEXOF = FUNCTION (ELEM) {// Alert ("indexof"); for (var i = 0; i = 0; i -) {if (ElementData [i] == ELEM) {RETURN I;}} return -1 }

THIS.CONTAINS = Function (ELEM) {Return this.indexof (elem)> = 0;

function ensureCapacity (minCapacity) {var oldCapacity = elementData.length; if (minCapacity> oldCapacity) {var oldData = elementData; var newCapacity = parseInt ((oldCapacity * 3) / 2 1); if (newCapacity

转载请注明原文地址:https://www.9cbs.com/read-112175.html

New Post(0)