Your task is to implement a Map data structure.
The keys are Strings and values
are Objects.
It must have the standard operations:
public void put(String key, Object Value)
public Object get(String key)
If the key used in put is already mapped, put should do nothing.
If the key used in
get does not exist, get should return null. There are no requirements on
performance.
For full points it must support any number of keys.
You are not allowed to import other classes. Hint: Use two arrays.
public class Map{
- String[] keys = new String[10];
- Object[] values = new String[10];
int size = 0;
public void put (String key, Object value){
- if (get(key)!=null) return;
- if (size >= keys.length) {
String[] newkeys = new String[keys.length*2];
Object[] newvalues = new Object[keys.length*2];
- for(int i = 0;i<size;i++){
- newkeys[i] = keys[i];
- newvalues[i] = values[i];
- }
- keys = newkeys;
- values = newvalues;
- }
- keys[size]=key;
- values[size]=value;
- size++;
- }
- public Object get(String key) {
- for(int i = 0;i<size;i++)
- if(keys[i].equals(key)) {return values[i];}
- return null;
- }
- }