59 個の要素 (各要素の値は整数) を持つハッシュ テーブルがあるとします。インデックス 15 は空白で、テーブルの残りの部分はデータでいっぱいです。挿入したい数によっては、二次プロービング式が要素 15 にヒットすることはありません。
数値 199 を挿入するとします (以下で使用している hashFunc() 関数を使用して 22 にハッシュする必要があります)。
public int hashFunc(int key)
{
return key % arraySize; //199 % 59 = 22
}
public void insert(DataItem item)
{
int key = item.getKey(); // extract the key (199)
int hashVal = hashFunc(key); // hash the key (22)
int i = 1;
//The while loop just checks that the array index isn't null and isn't equal to -1 which I defined to be a deleted element
while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1)
{
hashVal = hashFunc(key) + (i * i); //This never hits element 15!!!
i++;
hashVal %= arraySize; // wraparound when hashVal is beyond 59
}
hashArray[hashVal] = item; // insert item
}