js模擬C#中List的簡單實例
/*
* List 大小可變數(shù)組
* 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 < 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);
};
/**
* 返回此列表中的元素數(shù)。
* @return 元素數(shù)量
*/
List.prototype.size = function() {
return this.list.length;
};
/**
* 返回列表中指定的 start(包括)和 end(不包括)之間列表。
* @param start 開始位置
* @param end 結(jié)束位置
* @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;
};
相關(guān)文章
JavaScript讀取中文cookie時的亂碼問題的解決方法
讀取中文cookie時出現(xiàn)亂碼,下面是具體的解決方法,大家以后使用過程中,盡量不要用中文。2009-10-10js對列表中第一個值處理與jsp頁面對列表中第一個值處理的區(qū)別詳解
本文是對js對列表中第一個值處理與jsp頁面對列表中第一個值處理的區(qū)別進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11BootStrap Table后臺分頁時前臺刪除最后一頁所有數(shù)據(jù)refresh刷新后無數(shù)據(jù)問題
這篇文章主要介紹了BootStrap Table后臺分頁時前臺刪除最后一頁所有數(shù)據(jù)refresh刷新后無數(shù)據(jù)問題,需要的朋友可以參考下2016-12-12淺析JavaScript中的變量復制、參數(shù)傳遞和作用域鏈
這篇文章主要介紹了淺析JavaScript中的變量復制、參數(shù)傳遞和作用域鏈 的相關(guān)資料,需要的朋友可以參考下2016-01-01