【发布时间】:2013-11-29 15:49:57
【问题描述】:
我有下面的方法来获取在双散列类中输入的键的值。运行后一直说有错误。
/* Function to get value of a key */
public int get(String key)
{
int hash1 = myhash1( key );
int hash2 = myhash2( key );
while (table[hash1] != null && !table[hash1].key.equals(key))
{
hash1 += hash2;
hash1 %= TABLE_SIZE;
}
return table[hash1].value;
}
首先我必须在哈希表中插入一个新的名称和值,如果之后我有示例,则可以正常工作:
System.out.println( "Please enter the name of the person you want to search for: " );
System.out.println( "Value= " + ht.get(scan.next()));
但如果我有:
System.out.println( "Please enter the name of the person you want to search for: " );
System.out.println( "Value= " + ht.get(scan.nextLine()));
它说有一个错误。这意味着该方法不接受包含空格等的整行字符串,但它只接受单个字符串。 Netbeans 说错误在于这一行:
return table[hash1].value;
谁能帮帮我?
【问题讨论】:
-
它说什么?空指针异常?这意味着 table[hash1] 为空。如果它是 IndexOutOfBounds 则意味着 hash1 计算错误。我们需要更多信息在这里
-
@EdgarBoda 编译器没有告诉我,它只是说“在 HashTable.get(HashTable.java:73)”
-
@EdgarBoda 我用 java 博士再次尝试,它说 nullpointerexception
-
好的,我想说你的问题是
table[hash1]为空。当您在 while 循环的头部访问table[hash1]时,索引应该没问题。所以你应该检查为什么table[hash1]为空。 -
请发布您的程序的sscce。
标签: java hash double-hashing