...
This noncompliant code example uses the integer hashKey as an index into the hash array. A negative hash key produces a negative result from the remainder operator, causing the lookup() method to throw java.lang.ArrayIndexOutOfBoundsException.
| Code Block | ||
|---|---|---|
| ||
private int SIZE = 16;
public int[] hash = new int[SIZE];
public int lookup(int hashKey) {
return hash[hashKey % SIZE];
}
|
A negative hash key produces a negative result from the remainder operator, causing the lookup() method to throw java.lang.ArrayIndexOutOfBoundsException.
Compliant Solution
This compliant solution calls the imod() method that always returns a positive remainder:
...