【发布时间】:2014-05-05 21:40:58
【问题描述】:
所以我正在尝试使用链接列表数组来实现哈希表。为了练习,我实现了自己的链接列表,现在我正在尝试实现哈希表。现在散列函数非常简单,它只需要 char 的整数值并通过 hasharray 的 arraysize 对其进行修改。
在下面的代码中,我已经注释了它出错的地方。它似乎很好地散列了字符值,但是当我尝试将它插入到链接列表数组中时,它就崩溃了。
我已经测试了链接列表类并且工作正常。 我与 system.out.println 语句相处的错误是:
运行时错误时间:0.07 内存:380160 信号:-1
Created HashTable hashInsert hashFunction 2 hashFunctionAfter2f
Java 代码:对于class LinkListPractice:
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'));
}
}
Java 代码:对于class HashTable:
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;
}
}
Java 代码:对于class Link:
class Link{
public char value;
public Link next;
public Link prev;
public Link(char data){
this.value = data;
this.next = null;
this.prev = null;
}
}
Java 代码:对于class LinkList:
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;
}
}
}
}
【问题讨论】:
-
列出错误信息“可能”只是帮助别人。
-
很抱歉。当我运行它时,我得到这个:运行时错误时间:0.07 内存:380160 信号:-1 Created HashTable hashInsert hashFunction 2 hashFunctionAfter2f
-
我认为 Arraylist 数组创建存在一些问题。参考这个:stackoverflow.com/questions/15193229/…
-
当我声明 LinkLists 数组时: LinkList[] hashArray;然后在构造函数中初始化它: hashArray = new LinkList[size];它是否还调用该数组中所有 LinkList 对象的构造函数?我在想这可能是当我尝试调用 insertLink 方法时,没有构造实际的 LinkList 对象的问题。或者这是错误的,当我构造 hashArray = new LinkList[size] 时,它是否也调用了该数组中所有 LinkList 对象的构造函数??
标签: java hash linked-list hashtable