Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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_Hash_Linked List_Hashtable - Fatal编程技术网

Java 创建哈希表,但此时失败。需要帮助了解它失败的原因吗

Java 创建哈希表,但此时失败。需要帮助了解它失败的原因吗,java,hash,linked-list,hashtable,Java,Hash,Linked List,Hashtable,因此,我尝试使用链接列表数组实现一个哈希表。为了练习,我实现了自己的链接列表,现在我正在尝试实现哈希表。现在hash函数非常简单,它只接受char的整数值,并通过hasharray的arraysize对其进行修改 在下面的代码中,我已经注释了它出错的地方。它似乎很好地散列了字符值,但是当我尝试将其插入链接列表数组时,它就崩溃了 我已经测试了linklist类,效果很好。 我在system.out.println语句中遇到的错误是: 运行时错误时间:0.07内存:380160信号:-1 Creat

因此,我尝试使用链接列表数组实现一个哈希表。为了练习,我实现了自己的链接列表,现在我正在尝试实现哈希表。现在hash函数非常简单,它只接受char的整数值,并通过hasharray的arraysize对其进行修改

在下面的代码中,我已经注释了它出错的地方。它似乎很好地散列了字符值,但是当我尝试将其插入链接列表数组时,它就崩溃了

我已经测试了linklist类,效果很好。 我在system.out.println语句中遇到的错误是:

运行时错误时间:0.07内存:380160信号:-1

Created HashTable
hashInsert
hashFunction
2
hashFunctionAfter2f
Java代码:对于类链接列表实践

class LinkListPractice{

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

        HashTable testHashTable = new HashTable(10);
        System.out.println("Created HashTable");

        System.out.println(testHashTable.hashInsert('f'));

    }
}
class HashTable{

    LinkList[] hashArray;
    int arraySize;

    public HashTable(int size){
        hashArray = new LinkList[size];
        arraySize = size;
    }

    public int hashInsert(char value){
        System.out.println("hashInsert");
        int index = hashFunction(value);
        System.out.println("hashFunctionAfter"+index+value);
            /********************************************************
        code gets to here but it fails after printing the above.
        I think somehow the hashArray is not initialized 
            so it is failing trying to call the insertLink method
        can someone tell me why it is failing here?
            *********************************************************/
        hashArray[index].insertLink(value);

        return index;
    }

    public int hashFunction(char value){
        System.out.println("hashFunction");
        int index = (value % arraySize);
        System.out.println(index);

        return index;
    }

}
class Link{
    public char value;
    public Link next;
    public Link prev;

    public Link(char data){
        this.value = data;
        this.next = null;
        this.prev = null;
    }
}
class LinkList{
    public Link head;
    public Link tail;

    public LinkList(){
        head = null;
        tail = null;
    }

    public void insertLink(char data){
    System.out.println("insertLink" + data);

        Link newLink = new Link(data);

        if (head == null && tail == null){
            head = newLink;
            tail = newLink;
        }
        else if (head == tail){
            head.next = newLink;
            newLink.prev = head;
            tail = newLink;
        }
        else if (head != tail){
            tail.next = newLink;
            newLink.prev = tail;
            tail = newLink;
        }
    }

    public void printLinkList(){
        Link currentLink = head;

        while(currentLink != null){
            System.out.println(currentLink.value);
            currentLink = currentLink.next;
        }
    }

    public void printLinkListPrev(){
        Link currentLink = head;

        while(currentLink != null){
            if(currentLink.prev == null){
                System.out.println("HEAD");
                currentLink = currentLink.next;
            }
            else{
                System.out.println(currentLink.prev.value);
                currentLink = currentLink.next;
            }
        }
    }
}
Java代码:对于类哈希表

class LinkListPractice{

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

        HashTable testHashTable = new HashTable(10);
        System.out.println("Created HashTable");

        System.out.println(testHashTable.hashInsert('f'));

    }
}
class HashTable{

    LinkList[] hashArray;
    int arraySize;

    public HashTable(int size){
        hashArray = new LinkList[size];
        arraySize = size;
    }

    public int hashInsert(char value){
        System.out.println("hashInsert");
        int index = hashFunction(value);
        System.out.println("hashFunctionAfter"+index+value);
            /********************************************************
        code gets to here but it fails after printing the above.
        I think somehow the hashArray is not initialized 
            so it is failing trying to call the insertLink method
        can someone tell me why it is failing here?
            *********************************************************/
        hashArray[index].insertLink(value);

        return index;
    }

    public int hashFunction(char value){
        System.out.println("hashFunction");
        int index = (value % arraySize);
        System.out.println(index);

        return index;
    }

}
class Link{
    public char value;
    public Link next;
    public Link prev;

    public Link(char data){
        this.value = data;
        this.next = null;
        this.prev = null;
    }
}
class LinkList{
    public Link head;
    public Link tail;

    public LinkList(){
        head = null;
        tail = null;
    }

    public void insertLink(char data){
    System.out.println("insertLink" + data);

        Link newLink = new Link(data);

        if (head == null && tail == null){
            head = newLink;
            tail = newLink;
        }
        else if (head == tail){
            head.next = newLink;
            newLink.prev = head;
            tail = newLink;
        }
        else if (head != tail){
            tail.next = newLink;
            newLink.prev = tail;
            tail = newLink;
        }
    }

    public void printLinkList(){
        Link currentLink = head;

        while(currentLink != null){
            System.out.println(currentLink.value);
            currentLink = currentLink.next;
        }
    }

    public void printLinkListPrev(){
        Link currentLink = head;

        while(currentLink != null){
            if(currentLink.prev == null){
                System.out.println("HEAD");
                currentLink = currentLink.next;
            }
            else{
                System.out.println(currentLink.prev.value);
                currentLink = currentLink.next;
            }
        }
    }
}
Java代码:对于类链接

class LinkListPractice{

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

        HashTable testHashTable = new HashTable(10);
        System.out.println("Created HashTable");

        System.out.println(testHashTable.hashInsert('f'));

    }
}
class HashTable{

    LinkList[] hashArray;
    int arraySize;

    public HashTable(int size){
        hashArray = new LinkList[size];
        arraySize = size;
    }

    public int hashInsert(char value){
        System.out.println("hashInsert");
        int index = hashFunction(value);
        System.out.println("hashFunctionAfter"+index+value);
            /********************************************************
        code gets to here but it fails after printing the above.
        I think somehow the hashArray is not initialized 
            so it is failing trying to call the insertLink method
        can someone tell me why it is failing here?
            *********************************************************/
        hashArray[index].insertLink(value);

        return index;
    }

    public int hashFunction(char value){
        System.out.println("hashFunction");
        int index = (value % arraySize);
        System.out.println(index);

        return index;
    }

}
class Link{
    public char value;
    public Link next;
    public Link prev;

    public Link(char data){
        this.value = data;
        this.next = null;
        this.prev = null;
    }
}
class LinkList{
    public Link head;
    public Link tail;

    public LinkList(){
        head = null;
        tail = null;
    }

    public void insertLink(char data){
    System.out.println("insertLink" + data);

        Link newLink = new Link(data);

        if (head == null && tail == null){
            head = newLink;
            tail = newLink;
        }
        else if (head == tail){
            head.next = newLink;
            newLink.prev = head;
            tail = newLink;
        }
        else if (head != tail){
            tail.next = newLink;
            newLink.prev = tail;
            tail = newLink;
        }
    }

    public void printLinkList(){
        Link currentLink = head;

        while(currentLink != null){
            System.out.println(currentLink.value);
            currentLink = currentLink.next;
        }
    }

    public void printLinkListPrev(){
        Link currentLink = head;

        while(currentLink != null){
            if(currentLink.prev == null){
                System.out.println("HEAD");
                currentLink = currentLink.next;
            }
            else{
                System.out.println(currentLink.prev.value);
                currentLink = currentLink.next;
            }
        }
    }
}
Java代码:对于类链接列表

class LinkListPractice{

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

        HashTable testHashTable = new HashTable(10);
        System.out.println("Created HashTable");

        System.out.println(testHashTable.hashInsert('f'));

    }
}
class HashTable{

    LinkList[] hashArray;
    int arraySize;

    public HashTable(int size){
        hashArray = new LinkList[size];
        arraySize = size;
    }

    public int hashInsert(char value){
        System.out.println("hashInsert");
        int index = hashFunction(value);
        System.out.println("hashFunctionAfter"+index+value);
            /********************************************************
        code gets to here but it fails after printing the above.
        I think somehow the hashArray is not initialized 
            so it is failing trying to call the insertLink method
        can someone tell me why it is failing here?
            *********************************************************/
        hashArray[index].insertLink(value);

        return index;
    }

    public int hashFunction(char value){
        System.out.println("hashFunction");
        int index = (value % arraySize);
        System.out.println(index);

        return index;
    }

}
class Link{
    public char value;
    public Link next;
    public Link prev;

    public Link(char data){
        this.value = data;
        this.next = null;
        this.prev = null;
    }
}
class LinkList{
    public Link head;
    public Link tail;

    public LinkList(){
        head = null;
        tail = null;
    }

    public void insertLink(char data){
    System.out.println("insertLink" + data);

        Link newLink = new Link(data);

        if (head == null && tail == null){
            head = newLink;
            tail = newLink;
        }
        else if (head == tail){
            head.next = newLink;
            newLink.prev = head;
            tail = newLink;
        }
        else if (head != tail){
            tail.next = newLink;
            newLink.prev = tail;
            tail = newLink;
        }
    }

    public void printLinkList(){
        Link currentLink = head;

        while(currentLink != null){
            System.out.println(currentLink.value);
            currentLink = currentLink.next;
        }
    }

    public void printLinkListPrev(){
        Link currentLink = head;

        while(currentLink != null){
            if(currentLink.prev == null){
                System.out.println("HEAD");
                currentLink = currentLink.next;
            }
            else{
                System.out.println(currentLink.prev.value);
                currentLink = currentLink.next;
            }
        }
    }
}

您需要初始化
hashArray
数组中的对象

        hashArray[index] = new LinkList();
        hashArray[index].insertLink(value);
旁注:在文件中发表评论时,不要使用

/** text **/

因为这是为Javadoc保留的。

列出错误消息“可能”只会帮助其他人。对此表示抱歉。当我运行它时,我得到以下信息:运行时错误时间:0.07内存:380160信号:-1创建了哈希表hashInsert hashFunction 2 HashFunctionAfter2F我认为Arraylist数组创建存在一些问题。请参阅:当我声明链接列表数组时:LinkList[]hashArray;然后在构造函数中初始化它:hashArray=newlinklist[size];它是否也为该数组中的所有LinkList对象调用构造函数?我认为这可能是一个问题,当我试图调用insertLink方法时,实际的LinkList对象没有被构造。或者这是错误的,当我构造hashArray=newlinklist[size]时,它是否也会调用该数组中所有LinkList对象的构造函数??谢谢。这就是问题所在。出于某种原因,我认为当我创建并初始化一个链接列表数组时,它会调用每个链接列表的构造函数,但我想不会。再次感谢。数组中的对象在创建数组时不会初始化,原因有很多(比如它们可能是抽象类)。