/ ************************************************** ********* * 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
THISDEXOF = FUNCTION (ELEM) {// Alert ("indexof"); for (var i = 0; i
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