我正在为分配实现自己的 HashTable,我需要从文本文件中读取,然后将其内容添加到 HashTable。我遇到了几个问题,
1) 字符串的一些哈希值以负数出现。
2)有些词没有被添加。
这是我的哈希函数代码:
public int hash(String key) {
int hashkey = key.hashCode();
return hashkey % arraySize;
} // END hash()
对于computer
它返回整数的单词-97
,为了解决这个问题,我包含了一个 if 语句,如果整数为负数则使其为正数。但是,即使有这个词computer
,也不会在 index 处添加到我的 HashTable 中97
。
其他一些没有被添加的词是:tree, subtree, record, single
除其他外。这是我的文件 I/O 函数并将它们添加到哈希表中:
public static void readParagraph() {
BufferedReader reader;
String inputLine;
try {
reader = new BufferedReader(new FileReader(".\\src\\paragraph.txt"));
while((inputLine = reader.readLine()) != null) {
String[] strings = inputLine.split("(\\s|\\p{Punct})+");
insertParagraph(strings);
}
} catch (IOException e) {
System.out.println("Error: " + e);
}
} // END readParagraph()
private static void insertParagraph(String[] strings) {
Link link;
for (String string : strings) {
if (!string.equals("")) {
link = new Link(string.replaceAll("[^a-zA-Z'\\s]", "").toLowerCase());
paragraph.insert(link);
}
}
} // END insertParagraph()
有谁知道为什么没有添加一些单词有什么问题?或者为什么我得到哈希值的负数?
编辑:更多信息
public class A4Q3 {
static HashTable dictionary = new HashTable(1009);
static HashTable paragraph = new HashTable(164);
public static void main(String[] args) {
readParagraph();
paragraph.displayTable();
System.out.println();
System.out.println(paragraph.wordCount);
} // END main()
public void insert(Link link) {
String key = link.getKey();
Link previous = null;
Link current = first;
while(current != null && key.compareTo(current.getKey()) > 0) {
previous = current;
current = current.getNext();
}
if(previous == null) {
first = link;
} else {
previous.setNext(link);
link.setNext(current);
}
} // END insert()
链接类:
public class Link {
private String data;
private Link next;
public Link(String data) {
this.data = data;
} // END Link()
public String getKey() {
return data;
} // END getKey()
public void displayLink() {
System.out.print(data + " ");
} // END displayLink()
public Link getNext() {
return next;
} // END getNext()
public void setNext(Link next) {
this.next = next;
} // END setNext()
} // 结束链接