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