哈希表ArithmeticException

时间:2018-11-05 11:23:14

标签: java hashtable arithmetic-expressions

我当前正在编写一个哈希表,但是在测试时。它给我一个java.lang.ArithmeticException /零。 这是我的代码:

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

退货引起了问题。

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

2 个答案:

答案 0 :(得分:1)

您在许多地方计算了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;
    ...
}

答案 1 :(得分:1)

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

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