我無法弄清楚如何加倍散列表的大小。下面是代碼:卡在散列表的大小加倍
private void doubleLength() {
//Remember the old hash table array and allocate a new one 2 times as big
HashMap<K,V> resizedMap = new HashMap<K,V>(map.length * 2);
/*Traverse the old hash table adding each value to the new hash table.
Instead, add it to by applying
hashing/compression again (compression will be DIFFERENT, because the
length of the table is doubled, so we compute the same hash value but
compute the remainder using the DIFFERENT TABLE LENGTH).*/
for (int i = 0; i < map.length; i++) {
for (K key : map[i].entry) { //iterator does not work here
resizedMap.put(key, map[i].get(key)); //should go here
}
}
哈希表LN對象的數組,其中LN被定義爲:
public static class LN<K1,V1> {
public Map.Entry<K1,V1> entry;
public LN<K1,V1> next;
public LN (Map.Entry<K1,V1> e, LN<K1,V1> n)
{entry = e; next = n;}
}
我有類中可迭代但它不允許在地圖[ I] .entry.entries()。
public Iterable<Map.Entry<K,V>> entries() {
return new Iterable<Map.Entry<K,V>>() {
public Iterator<Map.Entry<K,V>> iterator() {
return new MapEntryIterator();
}
};
}
我很迷茫,我怎麼能把公共LN的大小加倍[] map;
我很困惑。後面的兩個代碼片斷表明你正在實現自己的散列表,但第一個代碼片斷表明你正在使用java.util.HashMap。 –