diff --git a/src/ch/zhaw/ads/MyHashtable.java b/src/ch/zhaw/ads/MyHashtable.java index bff0186..0b5f4fc 100644 --- a/src/ch/zhaw/ads/MyHashtable.java +++ b/src/ch/zhaw/ads/MyHashtable.java @@ -6,6 +6,7 @@ import java.util.*; public class MyHashtable implements Map { private K[] keys;// = (K[]) new Object[10]; private V[] values;// = (V[]) new Object[10]; + int initialSize; private int hash(Object k) { int h = Math.abs(k.hashCode()); @@ -14,8 +15,8 @@ public class MyHashtable implements Map { public MyHashtable(int size) { // to be done - keys = (K[]) new Object[size]; - values = (V[]) new Object[size]; + initialSize = size; + clear(); } public MyHashtable() { @@ -25,15 +26,25 @@ public class MyHashtable implements Map { // Removes all mappings from this map (optional operation). public void clear() { // to be done - keys = (K[]) new Object[1000]; - values = (V[]) new Object[1000]; + keys = (K[]) new Object[initialSize]; + values = (V[]) new Object[initialSize]; + } + + private void extendTable() { + K[] actualKeys = keys; + V[] actualValues = values; + keys = (K[]) new Object[actualKeys.length * 2]; + values = (V[]) new Object[actualKeys.length * 2]; + for(int i = 0; i < actualKeys.length; i++) { + put(actualKeys[i], actualValues[i]); + } } // Associates the specified value with the specified key in this map (optional operation). public V put(K key, V value) { // to be done if(size() >= keys.length) { - throw new IllegalStateException("Overflow"); + extendTable(); } else { int i = -1; int hashCode = hash(key);