js 实现list类
js中没有list类,可以使用Array来实现list类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
(function(win) { var ArrayList = function() { this.datas = []; }; var proto = ArrayList.prototype; proto.size = function() { return this.datas.length; }; proto.isEmpty = function() { return this.size() === 0; }; proto.contains = function(value) { //return this.datas.indexOf(value) !== -1; }; proto.indexOf = function(value) { for ( var index in this.datas) { if (this.datas[index] === value) { return index; } } return -1; }; proto.lastIndexOf = function(value) { for ( var index = this.size(); index >= 0; index--) { if (this.datas[index] === value) { return index; } } }; proto.toArray = function() { return this.datas; }; proto.outOfBound = function(index) { return index < 0 || index > (this.size() - 1); }; proto.get = function(index) { if (this.outOfBound(index)) { return null; } return this.datas[index]; }; proto.set = function(index, value) { this.datas[index] = value; }; proto.add = function(value) { this.datas.push(value); }; proto.insert = function(index, value) { if (this.outOfBound(index)) { return; } this.datas.splice(index, 0, value); }; proto.remove = function(index) { if (this.outOfBound(index)) { return false; } this.datas.splice(index, 1); return true; }; proto.removeValue = function(value) { if (this.contains(value)) { this.remove(this.indexOf(value)); return true; } return false; }; proto.clear = function() { this.datas.splice(0, this.size()); }; proto.addAll = function(list) { if (!list instanceof ArrayList) { return false; } for ( var index in list.datas) { this.add(list.get(index)); } return true; }; proto.insertAll = function(index, list) { if (this.outOfBound(index)) { return false; } if (!list instanceof ArrayList) { return false; } var pos = index; for(var index in list.datas) { this.insert(pos++, list.get(index)); } return true; }; function numberorder(a, b) { return a - b; } proto.sort = function(isNumber){ if(isNumber){ this.datas.sort(numberorder); return; } this.datas.sort(); }; proto.toString = function(){ return "[" + this.datas.join() + "]"; }; proto.valueOf = function(){ return this.toString(); }; win.ArrayList = ArrayList; })(window);
下面是测试页面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ArrayList Test</title> <script type="text/javascript" src="ArrayList.js"></script> <script type="text/javascript"> window.print = function(value) { //alert("window.print"); document.write(value); }; window.println = function(value) { //alert("window.println"); print(value); document.write("<br/>"); }; var list = new ArrayList(); list.add("jack"); list.add(43); list.add(true); println("----------------------"); println(list.get(0)); println(list.get(1)); println(list.get(2)); println(list.get(3)); println("----------------------"); println(list.size()); list.remove(2); println(list); println("----------------------"); println(list.isEmpty()); list.clear(); println(list.isEmpty()); println("----------------------"); list.add("jack"); list.add(43); list.add(true); var list2 = new ArrayList(); list2.addAll(list); println(list2); println("----------------------"); list2.insert(1,"male"); println(list2); println("----------------------"); list2.removeValue(true); println(list2); println("----------------------"); list2.insertAll(2,list); println(list2); println("----------------------"); println(list2.contains("jack")); println("----------------------"); list2.clear(); list2.add(1111); list2.add(222); list2.add(33); list2.add(4); list2.sort();//按字母顺序排 println(list2); println("----------------------"); list2.sort(true);//按数字顺序排 println(list2); println("----------------------"); </script> </head> <body> </body> </html>
测试结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
---------------------- jack 43 true null ---------------------- 3 [jack,43] ---------------------- false true ---------------------- [jack,43,true] ---------------------- [jack,male,43,true] ---------------------- [jack,male,43,true] ---------------------- [jack,male,jack,43,true,43,true] ---------------------- undefined ---------------------- [1111,222,33,4] ---------------------- [4,33,222,1111] ----------------------