Object[] tmp = new Object[i+2];
System.arraycopy(array, 0, tmp, 0, i);
tmp[i] = key;
tmp[i+1] = value;
table = tmp;
}
} else { // We are a hashtable
if ((size==ARRAY_BOUNDARY) && isArray()) {
grow();
}
((Hashtable)table).put(key, value);
}
}
}
/*
* Gets the value for key
*/
public Object get(Object key) {
Object value = null;
if (table !=null) {
if (isArray()) {
Object[] array = (Object[])table;
for (int i = 0; i<array.length-1; i+=2) {
if (array[i].equals(key)) {
value = array[i+1];
break;
}
}
} else {
value = ((Hashtable)table).get(key);
}
}
return value;
}
/*
* Returns the number of pairs in storage
*/
public int size() {
int size;
if (table==null)
return 0;
if (isArray()) {
size = ((Object[])table).length/2;
} else {
size = ((Hashtable)table).size();
}
return size;
}
/*
* Returns true if we have a value for the key
*/
public boolean containsKey(Object key) {
boolean contains = false;
if (table !=null) {
if (isArray()) {
Object[] array = (Object[])table;
for (int i = 0; i<array.length-1; i+=2) {
if (array[i].equals(key)) {
contains = true;
break;
}
}
} else {
contains = ((Hashtable)table).containsKey(key);
}
}
return contains;
}
/*
* Removes the key and its value
* Returns the value for the pair removed
*/
public Object remove(Object key){
Object value = null;
if (key==null) {
return null;
}
if (table !=null) {
if (isArray()){
// Is key on the list?
int index = -1;
Object[] array = (Object[])table;
for (int i = array.length-2; i>=0; i-=2) {
if (array[i].equals(key)) {
index = i;
value = array[i+1];
break;
}
}
// If so, remove it
if (index != -1) {
Object[] tmp = new Object[array.length-2];
// Copy the list up to index
System.arraycopy(array, 0, tmp, 0, index);
=2= |