2015-10-24 27 views

回答

1

這樣的工作,我覺得

Scanner scan = new Scanner("book.txt"); 

Set<String> list = new HashSet<String>(); 
String word = ""; 

while(scan.hasNextLine()) 
    list.add(scan.nextLine()); 
scan.close(); 
scan = new Scanner("input.txt"); 
while(scan.hasNextLine()) 
    if(!list.contains((word = scan.nextLine()))) //word from input.txt is not in book.txt 
     System.out.println(word); //print the word to the console 

scan.close(); 
+0

謝謝你的回答。不幸的是,我們不能使用Set list = new HashSet ();.有其他選擇嗎? –

+0

你在同一行有多個單詞嗎?爲什麼設置不允許? –

0

如果您不能使用一個HashSet然後創建一個屬於自己的hashFunction

大多采用散列函數是這樣的:

public static int mod = 1000007; 
private static Long hashFunction(String word) { 
     long hash = 5831; 
     for (int i = 0; i < word.length(); ++i) { 
      hash *= 33; 
      hash %= mod; 
      hash += word.charAt(i); 
      hash %= mod; 
     } 
     return hash; 
    } 

一個散列函數的基本想法是創建一個散列從它的詞,然後填寫該數組用布爾真,則表示它的存在。

當您創建一個包含所有散列值的整個hashTable時。然後,只需爲目標單詞創建一個散列並檢查它是否存在於hashTable中。

+0

這顯然是一項家庭作業。我確信這會在Java101中引起一片眉毛。 –