首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索HashTable

搜索HashTable
EN

Stack Overflow用户
提问于 2014-11-19 18:08:16
回答 1查看 225关注 0票数 0

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-19 21:29:42

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

我发现了SLLNode 这里的实现。不幸的是,类SLLNode没有任何公共方法/字段,因此应该将类添加到相同的包(或相同的文件)中。您可以通过链调用获得价值:

代码语言:javascript
复制
table1.search(res[0]).element.value
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27023896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档