Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 哈希表中的搜索函数_Java_Algorithm_Data Structures_Linked List_Hashtable - Fatal编程技术网

Java 哈希表中的搜索函数

Java 哈希表中的搜索函数,java,algorithm,data-structures,linked-list,hashtable,Java,Algorithm,Data Structures,Linked List,Hashtable,我不确定是否应该像这样创建Node[]size public class HashTable { /* Class containing next node and key and name value*/ class Node { private long key; public long getKey() { return key; } public void setKey(lo

我不确定是否应该像这样创建
Node[]size

public class HashTable {


    /* Class containing next node and key and name value*/
    class Node { 
        private long key;

        public long getKey() {
            return key;
        }

        public void setKey(long key) {
            this.key = key;
        }

        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        Node next; 

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        Node(long key, String value) {
            this.key = key;
            this.value = value;
            next = null;
        }
    } 
我的hashFunction(long key)可以工作,但是我在实现搜索函数的逻辑时遇到了问题。我不确定我做错了什么。有人能帮忙吗。
我的搜索实现从
公共节点搜索(长键)
开始,您使用的是节点
列表=大小[hash]
;在搜索中,但正如我在参数构造函数中看到的,您正在使用
array=newnode[size]来初始化它

因此,尝试用
数组[hash]
替换
大小[hash]


在粘贴的代码中,我没有看到任何使用
Node[]size
声明的情况。您可以删除size数组变量。

我认为在给定代码中没有。您是否管理了碰撞场景?如果您使用hash方法为两个不同的值返回相同的hashcode会怎么样。你是如何加入它并从中获得的?我只是不能说实现是正确的。对于初始的不同元素,它会起作用。我需要完整的实现来进一步检查
Node[] size;

    //The hash table array
    Node[] array = null;
    //hash table size. We typically choose m to be a power of 2
    int m = 0;

    // Constructor 
    HashTable(int size) {  
        m = size;
        array = new Node[size];  
    }

    HashTable() {  
        this(100);
    } 

    // Search for a given key in the hashtable
    // Note: you need to use hashFunction(long key) to determine the slot (array index) you will search in
    public Node search(long key) 
    { 
        Node node = null;
        /**
         * TODO
         */
        int hash = hashFunction(key);
        Node list = size[hash];
        while(list!=null) {
            if(list.getKey() == key) {
                return list;
            }
            else {
                list = list.getNext();
            }
        }
        return node;
    }