1![]()
2![]()
3
/**********************************************************
4
* JavaScript实现的ArrayList类
5
*
6
* @author {yangl}
7
* @version $Revision: 0.5 $ $Date: 2008/04/02 15:00:00 $
8
* @description
9
* Method:
10
* add(element);
11
* addElementAt(index, element);
12
* contains(element);
13
* get(index);
14
* isEmpty(index);
15
* indexOf(element);
16
* lastIndexOf(element);
17
* remove()
18
* setElementAt(index, element);
19
* size();
20
* toString();
21
* @example
22
* var arrList = new ArrayList();
23
* //var arrList = new ArrayList(10);
24
* arrList.add("000");
25
* arrList.add("001");
26
* arrList.add("002");
27
*
28
*********************************************************/
29
// JavaScript ArrayList
30
/**
31
Method:
32
add(element);
33
addElementAt(index, element);
34
contains(element);
35
get(index);
36
isEmpty(index);
37
indexOf(element);
38
lastIndexOf(element);
39
remove(index);
40
setElementAt(index, element);
41
size();
42
toString();
43
*/
44
/**
45
Example:
46
var arrList = new ArrayList();
47
//var arrList = new ArrayList(10);
48
arrList.add("000");
49
arrList.add("001");
50
arrList.add("002");
51
*/
52
var ArrayList = function () {
53
var args = ArrayList.arguments;
54
var initialCapacity = 10;
55
56
if (args != null && args.length > 0) {
57
initialCapacity = args[0];
58
}
59
60
var elementData = new Array(initialCapacity);
61
var elementCount = 0;
62
63
this.size = function () {
64
return elementCount;
65
};
66
67
this.add = function (element) {
68
//alert("add");
69
ensureCapacity(elementCount + 1);
70
elementData[elementCount++] = element;
71
return true;
72
};
73
74
this.addElementAt = function (index, element) {
75
//alert("addElementAt");
76
if (index > elementCount || index < 0) {
77
alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);
78
return;
79
//throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
80
}
81
ensureCapacity(elementCount + 1);
82
for (var i = elementCount + 1; i > index; i--) {
83
elementData[i] = elementData[i - 1];
84
}
85
elementData[index] = element;
86
elementCount++;
87
};
88
89
this.setElementAt = function (index, element) {
90
//alert("setElementAt");
91
if (index > elementCount || index < 0) {
92
alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);
93
return;
94
//throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
95
}
96
elementData[index] = element;
97
};
98
99
this.toString = function () {
100
//alert("toString()");
101
var str = "{";
102
for (var i = 0; i < elementCount; i++) {
103
if (i > 0) {
104
str += ",";
105
}
106
str += elementData[i];
107
}
108
str += "}";
109
return str;
110
};
111
112
this.get = function (index) {
113
//alert("elementAt");
114
if (index >= elementCount) {
115
alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);
116
return;
117
//throw ( new Error( -1,"ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount ) );
118
}
119
return elementData[index];
120
};
121
122
this.remove = function (index) {
123
if (index >= elementCount) {
124
alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);
125
//return;
126
throw (new Error(-1, "ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount));
127
}
128
var oldData = elementData[index];
129
for (var i = index; i < elementCount - 1; i++) {
130
elementData[i] = elementData[i + 1];
131
}
132
elementData[elementCount - 1] = null;
133
elementCount--;
134
return oldData;
135
};
136
137
this.isEmpty = function () {
138
return elementCount == 0;
139
};
140
141
this.indexOf = function (elem) {
142
//alert("indexOf");
143
for (var i = 0; i < elementCount; i++) {
144
if (elementData[i] == elem) {
145
return i;
146
}
147
}
148
return -1;
149
};
150
151
this.lastIndexOf = function (elem) {
152
for (var i = elementCount - 1; i >= 0; i--) {
153
if (elementData[i] == elem) {
154
return i;
155
}
156
}
157
return -1;
158
};
159
160
this.contains = function (elem) {
161
return this.indexOf(elem) >= 0;
162
};
163
164
function ensureCapacity(minCapacity) {
165
var oldCapacity = elementData.length;
166
if (minCapacity > oldCapacity) {
167
var oldData = elementData;
168
var newCapacity = parseInt((oldCapacity * 3) / 2 + 1);
169
if (newCapacity < minCapacity) {
170
newCapacity = minCapacity;
171
}
172
elementData = new Array(newCapacity);
173
for (var i = 0; i < oldCapacity; i++) {
174
elementData[i] = oldData[i];
175
}
176
}
177
}
178
};
179![]()

2

3
/**********************************************************4
* JavaScript实现的ArrayList类 5
* 6
* @author {yangl}7
* @version $Revision: 0.5 $ $Date: 2008/04/02 15:00:00 $8
* @description9
* Method:10
* add(element);11
* addElementAt(index, element);12
* contains(element);13
* get(index);14
* isEmpty(index);15
* indexOf(element);16
* lastIndexOf(element);17
* remove()18
* setElementAt(index, element);19
* size();20
* toString();21
* @example22
* var arrList = new ArrayList();23
* //var arrList = new ArrayList(10);24
* arrList.add("000");25
* arrList.add("001");26
* arrList.add("002");27
*28
*********************************************************/29
// JavaScript ArrayList30
/**31
Method:32
add(element);33
addElementAt(index, element);34
contains(element);35
get(index);36
isEmpty(index);37
indexOf(element);38
lastIndexOf(element);39
remove(index);40
setElementAt(index, element);41
size();42
toString();43
*/44
/**45
Example:46
var arrList = new ArrayList();47
//var arrList = new ArrayList(10);48
arrList.add("000");49
arrList.add("001");50
arrList.add("002");51
*/52
var ArrayList = function () {53
var args = ArrayList.arguments;54
var initialCapacity = 10;55
56
if (args != null && args.length > 0) {57
initialCapacity = args[0];58
}59
60
var elementData = new Array(initialCapacity);61
var elementCount = 0;62
63
this.size = function () {64
return elementCount;65
};66
67
this.add = function (element) {68
//alert("add");69
ensureCapacity(elementCount + 1);70
elementData[elementCount++] = element;71
return true;72
};73
74
this.addElementAt = function (index, element) {75
//alert("addElementAt");76
if (index > elementCount || index < 0) {77
alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);78
return;79
//throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));80
}81
ensureCapacity(elementCount + 1);82
for (var i = elementCount + 1; i > index; i--) {83
elementData[i] = elementData[i - 1];84
}85
elementData[index] = element;86
elementCount++;87
};88
89
this.setElementAt = function (index, element) {90
//alert("setElementAt");91
if (index > elementCount || index < 0) {92
alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);93
return;94
//throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));95
}96
elementData[index] = element;97
};98
99
this.toString = function () {100
//alert("toString()");101
var str = "{";102
for (var i = 0; i < elementCount; i++) {103
if (i > 0) {104
str += ",";105
}106
str += elementData[i];107
}108
str += "}";109
return str;110
};111
112
this.get = function (index) {113
//alert("elementAt");114
if (index >= elementCount) {115
alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);116
return;117
//throw ( new Error( -1,"ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount ) );118
}119
return elementData[index];120
};121
122
this.remove = function (index) {123
if (index >= elementCount) {124
alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);125
//return;126
throw (new Error(-1, "ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount));127
}128
var oldData = elementData[index];129
for (var i = index; i < elementCount - 1; i++) {130
elementData[i] = elementData[i + 1];131
}132
elementData[elementCount - 1] = null;133
elementCount--;134
return oldData;135
};136
137
this.isEmpty = function () {138
return elementCount == 0;139
};140
141
this.indexOf = function (elem) {142
//alert("indexOf");143
for (var i = 0; i < elementCount; i++) {144
if (elementData[i] == elem) {145
return i;146
}147
}148
return -1;149
};150
151
this.lastIndexOf = function (elem) {152
for (var i = elementCount - 1; i >= 0; i--) {153
if (elementData[i] == elem) {154
return i;155
}156
}157
return -1;158
};159
160
this.contains = function (elem) {161
return this.indexOf(elem) >= 0;162
};163
164
function ensureCapacity(minCapacity) {165
var oldCapacity = elementData.length;166
if (minCapacity > oldCapacity) {167
var oldData = elementData;168
var newCapacity = parseInt((oldCapacity * 3) / 2 + 1);169
if (newCapacity < minCapacity) {170
newCapacity = minCapacity;171
}172
elementData = new Array(newCapacity);173
for (var i = 0; i < oldCapacity; i++) {174
elementData[i] = oldData[i];175
}176
}177
}178
};179



浙公网安备 33010602011771号