您好,欢迎访问一九零五行业门户网

js模拟C#中List的简单实例_javascript技巧

复制代码 代码如下:
/*
 * list 大小可变数组
 * version: 1.0
 */
function list() {
    this.list = new array();
};/**
 * 将指定的元素添加到此列表的尾部。
 * @param object 指定的元素
 */
list.prototype.add = function(object) {
    this.list[this.list.length] = object;
};
/**
 * 将list添加到此列表的尾部。
 * @param listobject 一个列表
 */
list.prototype.addall = function(listobject) {
    this.list = this.list.concat(listobject.list);
};
/**
 *  返回此列表中指定位置上的元素。
 * @param index 指定位置
 * @return 此位置的元素
 */
list.prototype.get = function(index) {
    return this.list[index];
};
/**
 * 移除此列表中指定位置上的元素。
 * @param index 指定位置
 * @return 此位置的元素
 */
list.prototype.removeindex = function(index) {
    var object = this.list[index];
    this.list.splice(index, 1);   
    return object;
};
/**
 * 移除此列表中指定元素。
 * @param object 指定元素
 * @return 此位置的元素
 */
list.prototype.remove = function(object) {
    var i = 0;
    for(; 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);
};
/**
 * 返回此列表中的元素数。
 * @return 元素数量
 */
list.prototype.size = function() {
    return this.list.length;
};
/**
 * 返回列表中指定的 start(包括)和 end(不包括)之间列表。
 * @param start 开始位置
 * @param end   结束位置
 * @return  新的列表
 */
list.prototype.sublist = function(start, end) {   
    var list = new list();
    list.list = this.list.slice(start, end);
    return list;
};
/**
 *  如果列表不包含元素,则返回 true。
 * @return true or false
 */
list.prototype.isempty = function() {
    return this.list.length == 0;
};
其它类似信息

推荐信息