commonArrayList.js 2.4 KB

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