【发布时间】:2021-03-20 02:47:19
【问题描述】:
我正在尝试将文本文件中的关键字存储在哈希表中,然后读取另一个带有长字符串的文本文件,将其拆分为单词,将其放入数组中,然后循环以将哈希表值与数组值进行比较,如果等于或不是。
哈希表函数
sysout h 值的输出为:
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
我在 main 中用来比较文本文件的值和哈希表中的值的函数是
public static void main(String[] args) throws IOException {
hash_table keyword = new hash_table();
String text, t, thisline;
text = "";
BufferedReader br = new BufferedReader(new FileReader("words/TextFile.txt"));
while ((thisline = br.readLine()) != null) {
if (thisline.length() > 0) {
text += " " + thisline;
}
}
String[] array = text.split("\\ ", -1);
int len = array.length;
for (int i = 0; i < len; i++) {
t = array[i];
for (Map.Entry<String, String> entry : keyword.hashtable().entrySet()) {
if (entry.getValue().equals(t)) {
System.out.println("same");
}
}
}
}
另一件事,当我改变时
if (entry.getValue().equals(t)) {
与
if (!entry.getValue().equals(t)) {
它应该 sysout "same" 但它没有。
我试了好几个小时都没有成功,希望有人能帮忙!
【问题讨论】:
标签: java data-structures hashtable