gpt4 book ai didi

java - 搜索哈希表

转载 作者:行者123 更新时间:2023-12-01 12:15:41 25 4
gpt4 key购买 nike

在以下任务中,我必须将姓名放入哈希表中。名字是一个键,姓氏是一个值。之后我必须再次输入姓名。每次输入姓名后,我必须检查哈希表中是否存在相等的内容,如果相等,我必须打印类似“名字和姓氏相等”之类的内容。我必须使用我们教授给我们的数据结构进行散列,而不是从 Java 导入传统的散列。我的问题是我不知道如何搜索我的哈希表,我在 CBTH 类中有一个给定的搜索方法,我将把它放在我的代码下。

public class HashLozinki {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());

CBHT<Korisnici,String> table1 = new CBHT<Korisnici,String>(26);
for(int i=1;i<=N;i++){
String imelozinka = br.readLine();
String[] pom = imelozinka.split(" ");
table1.insert(new Korisnici(pom[0]), new String(pom[1]));
}

System.out.println(table1);

for(int i=1; i<=N; i++){
String korisnik = br.readLine();
String[] res = korisnik.split(" ");
table1.search(res[0]); // Here is my problem :S don't know how to use search
}

}
}

// The Search Method (part of CBTH class).. i don't know how to implement it
public SLLNode<MapEntry<K,E>> search(K targetKey) {
// Find which if any node of this CBHT contains an entry whose key is
// equal
// to targetKey. Return a link to that node (or null if there is none).
int b = hash(targetKey);
for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
return curr;
}
return null;
}

最佳答案

SLLNode必须有一个返回值的方法(或MapEntry)。

我找到了SLLNode的实现here 。不幸的是,类 SLLNode 没有任何公共(public)方法/字段,因此您应该将类​​添加到同一个包(或同一个文件)中。您可以通过链式调用获取值(value):

table1.search(res[0]).element.value

关于java - 搜索哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27023896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com