js中没有list类,可以使用Array来实现list类

  1. (function(win) {
  2. var ArrayList = function() {
  3. this.datas = [];
  4. };
  5.  
  6. var proto = ArrayList.prototype;
  7.  
  8. proto.size = function() {
  9. return this.datas.length;
  10. };
  11.  
  12. proto.isEmpty = function() {
  13. return this.size() === 0;
  14. };
  15.  
  16. proto.contains = function(value) {
  17. //return this.datas.indexOf(value) !== -1;
  18. };
  19.  
  20. proto.indexOf = function(value) {
  21. for ( var index in this.datas) {
  22. if (this.datas[index] === value) {
  23. return index;
  24. }
  25. }
  26.  
  27. return -1;
  28. };
  29.  
  30. proto.lastIndexOf = function(value) {
  31. for ( var index = this.size(); index >= 0; index--) {
  32. if (this.datas[index] === value) {
  33. return index;
  34. }
  35. }
  36. };
  37.  
  38. proto.toArray = function() {
  39. return this.datas;
  40. };
  41.  
  42. proto.outOfBound = function(index) {
  43. return index < 0 || index > (this.size() - 1);
  44. };
  45.  
  46. proto.get = function(index) {
  47. if (this.outOfBound(index)) {
  48. return null;
  49. }
  50.  
  51. return this.datas[index];
  52. };
  53.  
  54. proto.set = function(index, value) {
  55. this.datas[index] = value;
  56. };
  57.  
  58. proto.add = function(value) {
  59. this.datas.push(value);
  60. };
  61.  
  62. proto.insert = function(index, value) {
  63. if (this.outOfBound(index)) {
  64. return;
  65. }
  66.  
  67. this.datas.splice(index, 0, value);
  68. };
  69.  
  70. proto.remove = function(index) {
  71. if (this.outOfBound(index)) {
  72. return false;
  73. }
  74.  
  75. this.datas.splice(index, 1);
  76. return true;
  77. };
  78.  
  79. proto.removeValue = function(value) {
  80. if (this.contains(value)) {
  81. this.remove(this.indexOf(value));
  82. return true;
  83. }
  84. return false;
  85. };
  86.  
  87. proto.clear = function() {
  88. this.datas.splice(0, this.size());
  89. };
  90.  
  91. proto.addAll = function(list) {
  92. if (!list instanceof ArrayList) {
  93. return false;
  94. }
  95.  
  96. for ( var index in list.datas) {
  97. this.add(list.get(index));
  98. }
  99.  
  100. return true;
  101. };
  102.  
  103. proto.insertAll = function(index, list) {
  104. if (this.outOfBound(index)) {
  105. return false;
  106. }
  107.  
  108. if (!list instanceof ArrayList) {
  109. return false;
  110. }
  111.  
  112. var pos = index;
  113. for(var index in list.datas)
  114. {
  115. this.insert(pos++, list.get(index));
  116. }
  117. return true;
  118. };
  119.  
  120. function numberorder(a, b) {
  121. return a - b;
  122. }
  123.  
  124. proto.sort = function(isNumber){
  125. if(isNumber){
  126. this.datas.sort(numberorder);
  127. return;
  128. }
  129.  
  130. this.datas.sort();
  131. };
  132.  
  133. proto.toString = function(){
  134. return "[" + this.datas.join() + "]";
  135. };
  136.  
  137. proto.valueOf = function(){
  138. return this.toString();
  139. };
  140.  
  141. win.ArrayList = ArrayList;
  142. })(window);

ArrayList.js

下面是测试页面:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>ArrayList Test</title>
  7. <script type="text/javascript" src="ArrayList.js"></script>
  8. <script type="text/javascript">
  9. window.print = function(value) {
  10. //alert("window.print");
  11. document.write(value);
  12. };
  13.  
  14. window.println = function(value) {
  15. //alert("window.println");
  16. print(value);
  17. document.write("<br/>");
  18. };
  19.  
  20. var list = new ArrayList();
  21. list.add("jack");
  22. list.add(43);
  23. list.add(true);
  24. println("----------------------");
  25. println(list.get(0));
  26. println(list.get(1));
  27. println(list.get(2));
  28. println(list.get(3));
  29. println("----------------------");
  30.  
  31. println(list.size());
  32.  
  33. list.remove(2);
  34. println(list);
  35. println("----------------------");
  36.  
  37. println(list.isEmpty());
  38. list.clear();
  39. println(list.isEmpty());
  40. println("----------------------");
  41.  
  42. list.add("jack");
  43. list.add(43);
  44. list.add(true);
  45.  
  46. var list2 = new ArrayList();
  47. list2.addAll(list);
  48. println(list2);
  49. println("----------------------");
  50.  
  51. list2.insert(1,"male");
  52. println(list2);
  53. println("----------------------");
  54.  
  55. list2.removeValue(true);
  56. println(list2);
  57. println("----------------------");
  58.  
  59. list2.insertAll(2,list);
  60. println(list2);
  61. println("----------------------");
  62.  
  63. println(list2.contains("jack"));
  64. println("----------------------");
  65.  
  66. list2.clear();
  67. list2.add(1111);
  68. list2.add(222);
  69. list2.add(33);
  70. list2.add(4);
  71.  
  72. list2.sort();//按字母顺序排
  73. println(list2);
  74. println("----------------------");
  75.  
  76. list2.sort(true);//按数字顺序排
  77. println(list2);
  78. println("----------------------");
  79.  
  80. </script>
  81. </head>
  82. <body>
  83.  
  84. </body>
  85. </html>

ListTest.html

测试结果:

  1. ----------------------
  2. jack
  3. 43
  4. true
  5. null
  6. ----------------------
  7. 3
  8. [jack,43]
  9. ----------------------
  10. false
  11. true
  12. ----------------------
  13. [jack,43,true]
  14. ----------------------
  15. [jack,male,43,true]
  16. ----------------------
  17. [jack,male,43,true]
  18. ----------------------
  19. [jack,male,jack,43,true,43,true]
  20. ----------------------
  21. undefined
  22. ----------------------
  23. [1111,222,33,4]
  24. ----------------------
  25. [4,33,222,1111]
  26. ----------------------

js 实现list类的更多相关文章

  1. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  2. 通过寄生组合式继承创建js的异常类

    最近项目中在做js的统一的异常处理,需要自定义异常类.理想的设计方案为:自定义一个异常错误类BaseError,继承自Error,然后再自定义若干个系统异常,例如用户取消异常.表单异常.网络异常,这些 ...

  3. JS中定义类的方法

    JS中定义类的方式有很多种: 1.工厂方式    function Car(){     var ocar = new Object;     ocar.color = "blue" ...

  4. js模块,类,继承,命名空间,私有属性等相关概念梳理

    js确切的说是一种基于对象的语言,和纯面向对象的语言(比如as)稍微有点区别,js中没有类的概念.虽然有继承但是基于原型的继承.随着前段越来越受重视,jser们利用js的一些特性他们制造出了和纯面向对 ...

  5. JS中定义类的方法<转>

    转载地址:http://blog.csdn.net/sdlfx/article/details/1842218 PS(个人理解): 1) 类通过prototype定义的成员(方法或属性),是每个类对象 ...

  6. js集合set类的实现

    js集合set类的实现 /*js集合set类的实现*/ function Set() { this.dataStore = []; this.add = add;//新增元素 this.remove ...

  7. 使用js栈stack类的实现

    使用js栈stack类的实现 /*使用栈stack类的实现*/ function stack() { this.dataStore = [];//保存栈内元素,初始化为一个空数组 this.top = ...

  8. js中的类和对象以及自定义对象

    js中的类 1.类的声明 function Person(name,age){ this.name=name; this.age=age; this.test=function(a){ alert(a ...

  9. js中尺寸类样式

    js中尺寸类样式 一:鼠标尺寸类样式 都要事件对象的配合 Tip:注意与浏览器及元素尺寸分开,鼠标类尺寸样式都是X,Y,浏览器及元素的各项尺寸时Height,Width 1:检测相对于浏览器的位置:e ...

  10. koa 基础(十七)原生 JS 中的类、静态方法、继承

    1.app.js /** * 原生 JS 中的类.静态方法.继承 * es5中的类和静态方法 */ function Person(name, age) { // 构造函数里面的方法和属性 this. ...

随机推荐

  1. 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:

    在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...

  2. String的几种初始化方法的区别

    参考了: java中String的两种初始化方法   String a; String aa = ""; String aaa = "123"; String ...

  3. dorado7 重装了tomcat后配置路径

    在Windows->Preferences->Server->Runtime Environments把先前的工程Servers删除掉

  4. spring读取prperties配置文件(2)

    接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件. 这里我先定义好属性文件"user.propert ...

  5. The C in C++

    1 unnamed arguments in the argument list of the function definition (Page 114) In c++, an argument m ...

  6. SAP校园招聘笔试

    一直就向往着SAP公司,终于,有幸今天参加了SAP校园招聘的笔试.下面我就来简单说说这个笔试的内容. 笔试分为两大部分,一部分是逻辑题,就是些什么阅读分析计算balabala的一堆,是全英文的.另外一 ...

  7. 利用HibernateTools从数据库表生成带注解的POJO

    在SSH框架中,如果先设计好了数据库,那么下一步就需要从数据库Table生成实体java类和hbm.xml配置文件.在最新的开发框架中,已经支持使用注解,从而避免了繁琐的hbm.xml配置,而且我们可 ...

  8. stl::search

    template<class ForwardIt1, class ForwardIt2> ForwardIt1 search(ForwardIt1 first, ForwardIt1 la ...

  9. PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的安全通信,防止Secret Key盗用、数据篡改等恶意攻击行为

    简介 应用基于HTTP POST或HTTP GET请求发送Open API调用请求时,为了确保应用与REST服务器之间的安全通信,防止Secret Key盗用.数据篡改等恶意攻击行为,REST服务器使 ...

  10. [Oracle]Oracle学习小结(1)

    1.查看Oracle数据库中的所有用户: (1)使用具有DBA权限的账户登录数据库: (2)执行select username from dba_users. SQL> conn sys 输入口 ...