Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串哈希表实现_Java_Data Structures - Fatal编程技术网

Java 字符串哈希表实现

Java 字符串哈希表实现,java,data-structures,Java,Data Structures,对于我的赋值,我们需要创建一个字符串哈希表,我使用整数创建了一个哈希表,它工作得很好,但我现在遇到了麻烦 我一直在为输入的字符串获取java.lang.NumberFormatExceptions,我不知道为什么会发生这种情况。下面是我用于哈希表类的代码 public class HashTable { private SortedList[] hashArray; private int arraySize; public HashTable(int size) {

对于我的赋值,我们需要创建一个字符串哈希表,我使用整数创建了一个哈希表,它工作得很好,但我现在遇到了麻烦

我一直在为输入的字符串获取
java.lang.NumberFormatExceptions
,我不知道为什么会发生这种情况。下面是我用于哈希表类的代码

public class HashTable {
    private SortedList[] hashArray;
    private int arraySize;

    public HashTable(int size) {
        arraySize = size;
        hashArray = new SortedList[arraySize];

        for(int i = 0; i < arraySize; i++) {
            hashArray[i] = new SortedList();
        }
    } // END HashTable()

    public void displayTable() {
        for(int i = 0; i < arraySize; i++) {
            System.out.print(i + ". ");
            hashArray[i].displayList();
        }
    } // END displayTable()

    public int hash(String key) {
        int hashkey = Integer.parseInt(key);
        return hashkey % arraySize;
    } // END hash()

    public void insert(Link link) {
        String key = link.getKey();
        int hashVal = hash(key);
        hashArray[hashVal].insert(link);
    } // END insert()

    public void delete(String key) {
        int hashVal = hash(key);
        hashArray[hashVal].delete(key);
    } // END delete()
} // END HashTable
在hash函数中,在我的HashTable类中


编辑:据我所知,我需要将字符串转换为整数,然后执行哈希函数以获取其位置的索引。

要将
字符串
转换为整数以使用哈希键,请使用
键。hashCode()

要将
字符串
转换为整数以使用哈希键,请使用
键。hashCode()
看起来您正在尝试将字符串转换为整数,而字符串实际上并不表示数字

 int hashkey = Integer.parseInt(key);
如果您想获取字符串键的hashcode,只需调用

int hashkey = key.hashCode();

看起来您正在尝试将字符串转换为整数,而字符串实际上并不表示数字

 int hashkey = Integer.parseInt(key);
如果您想获取字符串键的hashcode,只需调用

int hashkey = key.hashCode();

您传递的字符串不是有效的整数字符串。根据:

抛出: -如果字符串不包含可解析整数


找到一种将字符串转换为整数的不同方法。提示:。

您传递的字符串不是有效的整数字符串。根据:

抛出: -如果字符串不包含可解析整数


找到一种将字符串转换为整数的不同方法。提示:.

因为
哈希表中的
链接.getKey()
key
变量。插入
哈希表。删除
允许传递所有
字符串。你可能应该使用

public int hash(String key) {
    int hashkey = key.hashCode();
    return hashkey % arraySize;
} // END hash()

因为您的
link.getKey()
key
变量位于
哈希表中。插入
哈希表。删除
允许传递所有
字符串。你可能应该使用

public int hash(String key) {
    int hashkey = key.hashCode();
    return hashkey % arraySize;
} // END hash()
我认为您需要编写一个from string->int,而不是试图将该键解释为十进制数字字符串,而这可能不是。您不需要“将字符串转换为整数”。您需要获得一个整数,该整数对于不同的字符串可能不同,对于具有相同字符序列的字符串肯定相同。String hashCode()结果非常适合于此。我认为您需要编写一个from String->int,而不是试图将键解释为十进制数字字符串,而不是可能不是的字符串。您不需要“将字符串转换为整数”。您需要获得一个整数,该整数对于不同的字符串可能不同,对于具有相同字符序列的字符串肯定相同。字符串hashCode()结果非常适合于此。