实现抽象类Dictionary

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Vector;


public class AssocArray extends Dictionary{
private Vector keys = new Vector();
private Vector values = new Vector();
@Override
public Enumeration elements() {
return values.elements();
}


@Override
public Object get(Object key) {
int index = keys.indexOf(key);
if(index == -1){
return null;
}else{
return values.elementAt(index);
}
}


@Override
public boolean isEmpty() {
return keys.isEmpty();
}


@Override
public Enumeration keys() {
return keys.elements();
}


@Override
public Object put(Object key, Object value) {
keys.addElement(key);
values.addElement(value);
return key;
}


@Override
public Object remove(Object key) {
int index = keys.indexOf(key);
if(index == -1){
return null;
}else{
keys.removeElementAt(index);
Object returnValue = values.elementAt(index);
values.removeElementAt(index);
return returnValue;
}
}


@Override
public int size() {
return keys.size();
}
public static void main(String[] args) {
AssocArray as = new AssocArray();
for(char c='a'; c<='z'; c++){
as.put(String.valueOf(c), String.valueOf(c).toUpperCase());
}
char[] ca = {'a', 'e', 'i', 'o', 'u'};
for(int i=0; iSystem.out.println("Uppercase :" + as.get(String.valueOf(ca[i])));
}
}
}


结果:

stk = [January, February, March, April, May, June, July, August, September, October, November, Docembetr]

element 5 = June
poping elements:
This is last line
Docembetr
November
October
September
August
July
June
May
April
March
February
January

你可能感兴趣的:(实现抽象类Dictionary)