哈希表算术异常

Hashtable ArithmeticException

我目前正在编写哈希 table,但是当我测试它时。它给我错误 java.lang.ArithmeticException / by zero。 这是我的代码:

   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;
    }

return 导致了问题。

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;
        }
    }
}

您在许多地方计算 x % maxSize,当 maxSize0 时,结果为 ArithmeticException

maxSize 初始化为正值。

注意以下内容:

public DSAHashTable()
{
    this.maxSize = maxSize;
    ...
}

相当于

public DSAHashTable()
{
    this.maxSize = this.maxSize;
    ...
}

这毫无意义。

要么接受 maxSize 的初始值作为构造函数参数:

public DSAHashTable(int maxSize)
{
    if (maxSize <= 0)
        throw new IllegalArgumentException("Illegal max size: " + maxSize);
    this.maxSize = maxSize;
    ...
}

或将其初始化为某个默认的正值:

static final int DEFAULT_MAX_SIZE = 10;

public DSAHashTable()
{
    this.maxSize = DEFAULT_MAX_SIZE;
    ...
}

看来错误在构造函数中。 您正在使用...本身初始化字段 maxSize。

您应该为构造函数提供一个 int 参数,以便您可以将非零值传递给 maxSize 字段。