JavaScript实现的ArrayList类[原创][2]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 yoinn 的 blog

        elementcount++;
    }

    this.setelementat = 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<elementcount;i++){
            if( i>0 ){
                str += ",";
            }
            str += elementdata[i];
        }
        str += "}";
        return str;
    }
   
    this.get = function(index){
        //alert("elementat");
        if ( index >= elementcount ) {
            alert( "arrayindexoutofboundsexception, " + index + " >= " + elementcount );
            return;
            //throw ( new error( -1,"arrayindexoutofboundsexception, " + index + " >= " + elementcount ) );
        }
        return elementdata[index];
    }
   
    this.remove = function(index){
        if ( index >= elementcount ) {
            alert( "arrayindexoutofboundsexception, " + index + " >= " + elementcount );
            //return;
            throw ( new error( -1,"arrayindexoutofboundsexception, " + index + " >= " + elementcount ) );
        }
        var olddata = elementdata[index];
        for( var i=index;i<elementcount - 1;i++ ){
            elementdata[i] = elementdata[i+1];    
        }
        elementdata[elementcount - 1] = null;
        elementcount--;
        return olddata;
    }

    this.isempty = function() {
        return elementcount == 0;
    }

    this.indexof = function(elem) {
        //alert("indexof");
        for ( var i=0; i < elementcount; i++ ){
            if (elementdata[i]==elem){
                return i;
            }
        }
        return -1;
    }

    this.lastindexof = function(elem) {
        for (var i = elementcount-1; i >= 0; i--){
            if ( elementdata[i]==elem ){
                return i;
            }
        }
        return -1;

本文关键:JavaScript实现的ArrayList类[原创]
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top