I'm currently writing a hash table, but when I test it. It gives me the error java.lang.ArithmeticException / by zero
.
This is my code:
private int hash(String key)
{
int hashIdx = 0;
int size = m_hashTable.length;
for (int i = 0; i < m_hashTable.length; i++)
{
hashIdx += key.charAt(i);
}
return hashIdx % maxSize;
}
The return is causing the problem.
import java.util.*;
public class DSAHashTable
{
private DSAHashEntry[] m_hashTable;
private int maxSize, size;
//contructor
public DSAHashTable()
{
this.maxSize = maxSize;
m_hashTable = new DSAHashEntry[maxSize];
for (int i = 0; i < m_hashTable.length; i++)
{
m_hashTable[i] = null;
}
}
//Adds new element
public void put(String key, Object value)
{
int tmp = hash(key);
int i = tmp;
do
{
if (m_hashTable[i] == null)
{
m_hashTable[i].setKey(key);
m_hashTable[i].setValue(value);
size++;
return;
}
else if (m_hashTable[i].equals(key))
{
m_hashTable[i].setValue(value);
return;
}
i = (i + 1) % maxSize;
}while (i != tmp);
}
public Object get(String key)
{
int i = hash(key);
while (m_hashTable[i] != null)
{
if (m_hashTable[i].equals(key))
{
return m_hashTable[i].getValue();
}
i = (i + 1) % maxSize;
}
return null;
}
public void remove(String key)
{
int i = hash(key);
while (!key.equals(m_hashTable[i].getKey()))
{
i = (i + 1) % maxSize;
}
for (i = (i + 1) % maxSize; m_hashTable[i] != null; i = (i + 1) % maxSize)
{
String tmp1 = m_hashTable[i].getKey();
Object tmp2 = m_hashTable[i].getValue();
m_hashTable[i] = null;
size--;
put(tmp1, tmp2);
}
size--;
}
public int size()
{
return size;
}
public boolean containsKey(String key)
{
return get(key) != null;
}
private void reSize(int size)
{
DSAHashEntry[] newTable = new DSAHashEntry[size];
for (int i = 0; i < maxSize; i++)
{
newTable[i] = null;
}
}
//Linear probing
private int hash(String key)
{
int hashIdx = 0;
int size = m_hashTable.length;
for (int i = 0; i < m_hashTable.length; i++)
{
hashIdx += key.charAt(i);
}
return hashIdx % maxSize;
}
public class DSAHashEntry
{
public String key;
public Object value;
public Integer state;
//contructor
//default
public DSAHashEntry()
{
key = "";
value = null;
}
public DSAHashEntry(String inKey, Object inValue)
{
this.key = key;
this.value = value;
this.state = 0;
}
//getters
public String getKey()
{
return key;
}
public Object getValue()
{
return value;
}
//setters
public void setKey (String inKey)
{
key = inKey;
}
public void setValue (Object inValue)
{
value = inValue;
}
//toString
public String toString()
{
return key + value;
}
}
}
You compute x % maxSize
in many places, and when maxSize
is 0
, this results in ArithmeticException
.
Initialize maxSize
to a positive value.
Note that the following :
public DSAHashTable()
{
this.maxSize = maxSize;
...
}
Is equivalent to
public DSAHashTable()
{
this.maxSize = this.maxSize;
...
}
which makes no sense.
Either accept the initial value of maxSize
as a constructor argument:
public DSAHashTable(int maxSize)
{
if (maxSize <= 0)
throw new IllegalArgumentException("Illegal max size: " + maxSize);
this.maxSize = maxSize;
...
}
or initialize it to some default positive value:
static final int DEFAULT_MAX_SIZE = 10;
public DSAHashTable()
{
this.maxSize = DEFAULT_MAX_SIZE;
...
}