首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >哈希表ArithmeticException

哈希表ArithmeticException
EN

Stack Overflow用户
提问于 2018-11-05 11:23:13
回答 2查看 222关注 0票数 0

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

代码语言:javascript
复制
   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;
    }

返回是造成问题的原因。

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-05 11:26:10

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

maxSize初始化为正值。

请注意以下几点:

代码语言:javascript
复制
public DSAHashTable()
{
    this.maxSize = maxSize;
    ...
}

等于

代码语言:javascript
复制
public DSAHashTable()
{
    this.maxSize = this.maxSize;
    ...
}

这毫无意义。

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

代码语言:javascript
复制
public DSAHashTable(int maxSize)
{
    if (maxSize <= 0)
        throw new IllegalArgumentException("Illegal max size: " + maxSize);
    this.maxSize = maxSize;
    ...
}

或将其初始化为某些默认值:

代码语言:javascript
复制
static final int DEFAULT_MAX_SIZE = 10;

public DSAHashTable()
{
    this.maxSize = DEFAULT_MAX_SIZE;
    ...
}
票数 2
EN

Stack Overflow用户

发布于 2018-11-05 11:26:48

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53153440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档