Js simulates a simple instance of a List in c-sharp

  • 2020-03-30 02:15:05
  • OfStack



function List() {
    this.list = new Array();
};

List.prototype.add = function(object) {
    this.list[this.list.length] = object;
};

List.prototype.addAll = function(listObject) {
    this.list = this.list.concat(listObject.list);
};

List.prototype.get = function(index) {
    return this.list[index];
};

List.prototype.removeIndex = function(index) {
    var object = this.list[index];
    this.list.splice(index, 1);    
    return object;
};

List.prototype.remove = function(object) {
    var i = 0;
    for(; i < this.list.length; i++) {        
        if( this.list[i] === object) {
            break;
        }        
    } 
    if(i >= this.list.length) {
        return null;
    } else {
        return this.removeIndex(i);
    }
};

List.prototype.clear = function() {
    this.list.splice(0, this.list.length);
};

List.prototype.size = function() {
    return this.list.length;
};

List.prototype.subList = function(start, end) {    
    var list = new List();
    list.list = this.list.slice(start, end);
    return list;
};

List.prototype.isEmpty = function() {
    return this.list.length == 0;
};


Related articles: