StringArray.java
4.53 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.emercit.model;
import java.io.*;
import java.util.*;
/**
* This class is an auto-resizable String array. It has similar methods to ArrayList
*
* @author Henry Zheng
* @url http://www.ireasoning.com
*/
public class StringArray implements Serializable
{
public static final int DEFAULT_CAPACITY = 10;
protected String[] _strings = null;
protected int _upperBound = 0;
protected int _capacity = DEFAULT_CAPACITY;
protected int _initialSize = _capacity;
protected float _loadFactory = 1.5F;
public StringArray ()
{
this(DEFAULT_CAPACITY);
}
public StringArray( int size)
{
_capacity = size;
_initialSize = size;
_strings = new String[size];
}
public synchronized void ensureCapacity(int capacity)
{
if(_capacity < capacity)
{
_capacity = (_capacity * 3)/2 + 1;
if(_capacity < capacity)
{
_capacity = capacity;
}
String [] oldData = _strings;
_strings = new String[_capacity];
System.arraycopy(oldData, 0, _strings, 0, _upperBound);
}
}
public synchronized void add(String s)
{
if(_upperBound == _capacity )
{
resize((int) (_capacity * _loadFactory));
}
_strings[_upperBound++] = s;
}
public synchronized void add(StringArray sa)
{
for (int i = 0; i < sa.size() ; i++)
{
add(sa.get(i));
}
}
public synchronized String get(int index)
{
return _strings[index];
}
public synchronized void set(int index, String newVal)
{
_strings[index] = newVal;
}
/** Adds all elements in passed string array */
public synchronized void add(String [] strs)
{
for (int i = 0; i < strs.length ; i++)
{
add(strs[i]);
}
}
/** Resets this object. */
public synchronized void clear()
{
_capacity = _initialSize;
_strings = new String[_capacity];
_upperBound = 0;
}
public synchronized String remove(int index)
{
if(index >= _upperBound )
{
throw new IndexOutOfBoundsException();
}
String s = _strings[index];
for (int i = index; i < _upperBound - 1 ; i++)
{
_strings[i] = _strings[i + 1];
}
_upperBound --;
return s;
}
/**
* Removes the first occurance of passed str
* @return the string removed, or null if not found
*/
public synchronized String remove(String str)
{
for (int i = 0; i < _upperBound ; i++)
{
if(_strings[i].equals(str))
{
return remove(i);
}
}
return null;
}
public synchronized int size()
{
return _upperBound;
}
public synchronized boolean isEmpty()
{
return _upperBound == 0;
}
public synchronized String[] toArray()
{
String [] ret = new String[_upperBound];
for (int i = 0; i < _upperBound ; i++)
{
ret[i] = _strings[i];
}
return ret;
}
protected synchronized void resize(int newCapacity)
{
String [] as = new String[newCapacity];
for (int i = 0; i < _strings.length ; i++)
{
as[i] = _strings[i];
}
_strings = as;
_capacity = newCapacity;
}
public String toString()
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < _upperBound ; i++)
{
buf.append(_strings[i] + "\n");
}
return buf.toString();
}
public static void main(String[] args)
{
StringArray as = new StringArray();
String [] ss = null;
ss = as.toArray();
// System.out.println( "ss len="+ss.length);
// System.out.println( "ss = " + ss);
for (int i = 0; i < 10 ; i++)
{
as.add("" + i);
}
// System.out.println( "size = " + as.size());
ss = as.toArray();
for (int i = 0; i < ss.length ; i++)
{
// System.out.println( ss[i]);
}
// System.out.println( "remove 5th element.");
as.remove(5);
// System.out.println( "size = " + as.size());
ss = as.toArray();
for (int i = 0; i < ss.length ; i++)
{
// System.out.println( ss[i]);
}
}
}//end of class StringArray