簡體   English   中英

為什么哈希表是空的?

[英]Why hashtable is empty?

我正在嘗試將文本文件中的關鍵字存儲在哈希表中,然后讀取另一個帶有長字符串的文本文件,將其拆分為單詞,將其放入數組中,然后循環比較哈希表值與數組值是否相等。

hash 表 function

sysout h 值的 output 為:

{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}

我在 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)) {

它應該系統輸出"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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM