在以下任务中,我必须将姓名和姓氏放入哈希表中。名称是一个键,姓是一个值。在那之后,我必须再次输入姓名和姓氏。每次输入姓名和姓氏后,我必须检查哈希表中是否有相同的名称,如果是相等的,我必须打印‘有相同的名字和姓氏’之类的东西。我必须使用数据结构来进行散列,这是教授给我们的,而不是从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;
}发布于 2014-11-19 21:29:42
类SLLNode必须有一个返回值的方法(或MapEntry)。
我发现了SLLNode 这里的实现。不幸的是,类SLLNode没有任何公共方法/字段,因此应该将类添加到相同的包(或相同的文件)中。您可以通过链调用获得价值:
table1.search(res[0]).element.valuehttps://stackoverflow.com/questions/27023896
复制相似问题