Java 二次探测哈希表
Hellow,由于某些原因,当我插入时,我无法用项目和键填充哈希表。它看起来像是在运行驱动程序时添加的,但是没有存储任何内容,并且绝对没有错误消息。 我假设创建一个利用二次探测的哈希表。我要计算横截数,横截数不能大于20 代码如下:Java 二次探测哈希表,java,hashtable,quadratic-probing,Java,Hashtable,Quadratic Probing,Hellow,由于某些原因,当我插入时,我无法用项目和键填充哈希表。它看起来像是在运行驱动程序时添加的,但是没有存储任何内容,并且绝对没有错误消息。 我假设创建一个利用二次探测的哈希表。我要计算横截数,横截数不能大于20 代码如下: public class HashTable { String[] table; String[] keys; int currentSize, maxSize, numOfEntries; int traverse = 0;
public class HashTable {
String[] table;
String[] keys;
int currentSize, maxSize, numOfEntries;
int traverse = 0;
double capacity;
public HashTable(int size, double load){
if(size <= 0){
System.out.println("Size must be greater then 0");
}
else if(load < 0 || load > 1){
System.out.println("Load must be between 0 and 1. EX: 0.75");
}else{
this.currentSize = 0;
table = new String[size];
keys = new String[size];
this.capacity = load * size;
this.maxSize = size;
}
}
public int hash(String num){
return (2 * Integer.parseInt(num) + 5) % table.length;
}
public int getLength(){
return table.length;
}
public int getMaxSize(){
return maxSize;
}
public int probe(String num){
int temp = hash(num);
int calc = 0;
numOfEntries = 0;
while(table[temp] != null){
traverse++;
temp = (int)((temp + (float)calc/2 + (float) (calc * calc)) % maxSize);
calc++;
numOfEntries++;
}
if(traverse >= 20){
System.out.println("Insert Failed : Reached 20 Traversal Limit!");
return 20;
}
return temp;
}
public void resize(){
String [] tempTable = table;
if(table.length >= capacity){
table = new String[tempTable.length * 2];
for(int i = 0; i < tempTable.length; i++){
if(tempTable[i] != null){
insert(tempTable[i]);
}
}
}
}
public void insert(String num){
int temp = probe(num);
table[temp] = num;
currentSize++;
if(currentSize >= capacity){
resize();
}
}
public String searchKey(String key){
String temp = "";
numOfEntries = 0;
for(int i = 0; i < keys.length; i++){
if(key == keys[i]){
temp = table[i];
numOfEntries++;
break;
}
numOfEntries++;
}
if(temp == ""){
System.out.println("No items that match that key!");
}
return temp;
}
public int numOfEntries(){
return numOfEntries;
}
public void print(){
System.out.println("Key\t:\tValue");
for(int i = 0; i < table.length; i++){
if(keys[i] != null){
System.out.println(keys[i] + "\t:\t" + table[i]);
}
}
System.out.println();
}
}您试过调试此代码吗?是的,我试过。调试器中也没有显示任何内容。这就像程序运行得很好,但没有存储任何东西………尝试在insert()方法中插入断点,逐步检查它的工作方式,找到java机器和/或其核心库的一些无法解释的行为,并在此处发布一些不合逻辑的东西的确切位置。请注意,在Java中,您应该将字符串与String.equals方法进行比较,因为运算符==在且仅当您将字符串与自身进行比较时返回true,否则它总是返回false,即使两个字符串的内容相同。现在,我的搜索方法出现错误。它说在like上有一个空指针异常:Java中的字符串是对象。通过引用传递的对象。引用可以为null,这意味着引用不引用任何内容。当然,如果
str
是空引用,则不能调用str.equals(otherString)
。otherString
可以为空,equals(otherString)
在这种情况下将返回false。使用类似于if(str!=null&&str.equals(otherString)){}
import java.util.*;
public class HashTableDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Table Size:\t");
int size = scan.nextInt();
System.out.println("Enter Load Factor (Between 0 and 1");
double load = scan.nextDouble();
int temp = 0;
HashTable table = new HashTable(size,load);
System.out.println(table.getLength());
System.out.println(table.getMaxSize());
while(temp != 4){
System.out.println();
System.out.println("i)(1) Insert an item to the Hash Table" +
"\nii)(2) Search A Specific Key" +
"\niii)(3) Display the Table" +
"\nvii)(4) Quit");
String input = scan.next();
switch(input){
case "1":
System.out.println();
System.out.println("Enter value to add to table");
String desKey1 = scan.next();
table.insert(desKey1);
continue;
case "2":
System.out.println("Please enter specific key desired:\t");
String desKey2 = scan.next();
if(table.searchKey(desKey2).equals(desKey2)){
System.out.println("Key Found");
System.out.println("Number of cells accessed:\t" + table.numOfEntries());
}
else{
System.out.println("Key Not Found");
}
continue;
case "3":
table.print();
continue;
case "4":
temp = 4;
break;
}
}
}