[英]Displaying search results from a hash table
I can't figure out how to display the results from my search method. 我不知道如何显示搜索方法的结果。 I put strings in where it needs to display the results. 我将字符串放在需要显示结果的地方。
Here is the client class 这是客户班
public class Client
{
private String name;
private String city;
public Client(String name, String city)
{
this.name = name;
this.city = city;
}
public String toString()
{
return name + " " + city;
}
public String getName()
{
return name;
}
public String getCity()
{
return city;
}
}
Here is my Hashtable class 这是我的哈希表课程
public class Hashtable {
private int n;
private Client[] table;
public Hashtable(int n) {
this.n = n;
table = new Client[n];
}
public int hashFunction(String key) {
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum += (int) key.charAt(i);
}
sum = sum%n;
return sum;
}
public String search(String key) {
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum += (int) key.charAt(i);
}
sum = sum%n;
if (key.equals(table[sum])) {
return ("Returns the toString from the client class here");
} else if (table[sum] == null) {
return null;
} else {
while (table[sum] != null) {
sum++;
}
return ("Returns the toString from the client class here");
}
}
public boolean insert(Client myClient) {
int counter = 0;
String temp = myClient.getName();
boolean ret = false;
int tempSum = 0;
for (int i = 0; i < temp.length(); i++) {
tempSum += (int) temp.charAt(i);
}
tempSum = tempSum%n;
if (table[tempSum] == null) {
table[tempSum] = myClient;
ret = true;
} else {
while (table[tempSum] != null) {
if(tempSum == table.length){
tempSum = -1;
}
tempSum++;
counter++;
}
if(counter != n){
ret = true;
table[tempSum] = myClient;
}
}
return ret;
}
}
When you are comparing the key in search method, Make sure you compare against name. 比较搜索方法中的关键字时,请确保与名称进行比较。
Change table[sum] to table[sum].getName() ???? 将表[sum]更改为表[sum] .getName()???? I modified the code and added comments for the change. 我修改了代码,并为更改添加了注释。
public class Hashtable {
private int n;
private Client[] table;
public Hashtable(int n) {
this.n = n;
table = new Client[n];
}
public int hashFunction(String key) {
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum += (int) key.charAt(i);
}
sum = sum%n;
return sum;
}
public String search(String key) {
int sum = 0;
for (int i = 0; i < key.length(); i++) {
sum += (int) key.charAt(i);
}
sum = sum%n;
if (key.equals(table[sum].getName())) { //This Should be table[sum].getName()
return ("Returns the toString from the client class here");
} else if (table[sum] == null) {
return null;
} else {
while (table[sum] != null) {
sum++;
}
return ("Returns the toString from the client class here");
}
}
public boolean insert(Client myClient) {
int counter = 0;
String temp = myClient.getName();
boolean ret = false;
int tempSum = 0;
for (int i = 0; i < temp.length(); i++) {
tempSum += (int) temp.charAt(i);
}
tempSum = tempSum%n;
if (table[tempSum] == null) {
table[tempSum] = myClient;
ret = true;
} else {
while (table[tempSum] != null) {
if(tempSum == table.length){
tempSum = -1;
}
tempSum++;
counter++;
}
if(counter != n){
ret = true;
table[tempSum] = myClient;
}
}
return ret;
}
}
Let me know if that helped 让我知道是否有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.