展会信息港展会大全

js模拟C#中List的简单实例
来源:互联网   发布日期:2016-01-27 15:40:33   浏览:1912次  

导读:本篇文章主要是对js模拟C 中List的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助代码如下: ** List 大小可变数组* version: 1 0* function List() { this list = new Array(); ...

本篇文章主要是对js模拟C#中List的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

代码如下:

/*

* 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 < 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);

};

/**

* 返回此列表中的元素数。

* @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;

};

赞助本站

人工智能实验室

相关热词: List js

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港