为什么哈希表是空的?
Why hashtable is empty?
我正在尝试将文本文件中的关键字存储在散列中table然后读取另一个包含长字符串的文本文件,将其拆分为单词,将其放入数组中,然后循环比较散列table values with the array values if equals or not.
散列table函数
sysout h值的输出为:
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
我在 main 中用来将文本文件的值与散列中的值进行比较的函数table是
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)) {
它应该系统输出 "same"
但它没有。
我试了好几个小时都没有成功,希望有人能帮忙!
替换此代码
while ((thisline = br.readLine()) != null) { // Will Iterate until br.readLine returns null
//System.out.println(thisline);
}
while (thisline != null) { // this variable is null, so, this chunk of code is never executed.
line = br.readLine();
h.put("" + i, line);
i++;
}
System.out.println(h); // retrun empty
return h;
有了这个
while ((thisline = br.readLine()) != null) {
h.put("" + i, thisline);
i++;
}
System.out.println(h);
return h;
我正在尝试将文本文件中的关键字存储在散列中table然后读取另一个包含长字符串的文本文件,将其拆分为单词,将其放入数组中,然后循环比较散列table values with the array values if equals or not.
散列table函数
sysout h值的输出为:
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
我在 main 中用来将文本文件的值与散列中的值进行比较的函数table是
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)) {
它应该系统输出 "same"
但它没有。
我试了好几个小时都没有成功,希望有人能帮忙!
替换此代码
while ((thisline = br.readLine()) != null) { // Will Iterate until br.readLine returns null
//System.out.println(thisline);
}
while (thisline != null) { // this variable is null, so, this chunk of code is never executed.
line = br.readLine();
h.put("" + i, line);
i++;
}
System.out.println(h); // retrun empty
return h;
有了这个
while ((thisline = br.readLine()) != null) {
h.put("" + i, thisline);
i++;
}
System.out.println(h);
return h;