/* Copyright (c) 2000 Stepping Stone Software Ltd, John Keyes */
public class StringVector {
private String [] data;
private int count;
public StringVector() { this(10); // default size is 10 }
public StringVector(int initialSize) {
data = new String[initialSize];
}
public void add(String str) {
// ignore null strings
if(str == null) { return; }
ensureCapacity(count + 1);
data[count++] = str;
}
private void ensureCapacity(int minCapacity) {
int oldCapacity = data.length;
if (minCapacity > oldCapacity) {
String oldData[] = data;
int newCapacity = oldCapacity * 2;
data = new String[newCapacity];
System.arraycopy(oldData, 0, data, 0, count);
}
}
public void remove(String str) {
if(str == null) { return // ignore null str }
for(int i = 0; i < count; i++) {
// check for a match
if(data[i].equals(str)) {
System.arraycopy(data,i+1,data,i,count-1); // copy data
// allow previously valid array element be gc'd
data[--count] = null;
return;
}
}
}
public final String getStringAt(int index) {
if(index < 0) { return null; }
else if(index > count) {
return null; // index is > # strings
}
else { return data[index]; // index is good }
}
// not shown: size(), toStringArray()
}