2012-12-12 95 views
1

大家好我爲一個字符串數組寫了一個mergesort程序,它從用戶讀入.txt文件。但我現在想要做的是比較兩個文件並打印出文件一中的文字而不是文件二中的文字,例如蘋果在文件1中但不是文件2.我試圖再次將它存儲在字符串數組中,然後將其打印出來最後,但我似乎不能實現它。 這是我的,Java String Array Mergesort

FileIO reader = new FileIO(); 
    String words[] = reader.load("C:\\list1.txt"); 
    String list[] = reader.load("C:\\list2.txt"); 

    mergeSort(words); 
    mergeSort(list); 
    String x = null ; 

    for(int i = 0; i<words.length; i++) 
    { 
     for(int j = 0; j<list.length; j++) 
     { 
       if(!words[i].equals(list[j])) 
       { 
         x = words[i]; 
       } 
     } 
    } 

    System.out.println(x); 

任何幫助或建議將appriciated!

+0

決不initalize你的串x與空,如果這兩個文件將是空的,或只是字[]文件,你會得到一個NullPointerException異常。請說'String x =「」;'。我不能真正得到你想要的東西。你想打印出每一個字,這是不是你的第二個文件? – SomeJavaGuy

回答

1

如果你想檢查的第一陣列中,但在第二個不存在的話,你可以這樣做:

boolean notEqual = true;   
for(int i = 0; i<words.length; i++) 
    { 
     for(int j = 0; j<list.length && notEqual; j++) 
     { 
       if(words[i].equals(list[j]))  // If the word of file one exist 
       {        // file two we set notEqual to false 
         notEqual = false;   // and we terminate the inner cycle 
       } 
     } 
     if(notEqual)      // If the notEqual remained true 
      System.out.println(words[i]); // we print the the element of file one 
              // that do not exist in the second file 

     notEqual = true;     // set variable to true to be used check 
    }          // the other words of file one. 

基本上,你的第一個文件需要一個字(串從數組中)並檢查文件2中是否有相同的單詞。如果找到它,則將控制變量notEqual設置爲false,從而退出內部循環並不打印該單詞。否則,如果文件2中沒有與文件1中的單詞相匹配的任何字,則控制變量notEqual將爲true。因此,打印內部循環外部的元素。

如果需要,您可以將打印語句替換爲另一個將唯一字存儲在額外數組中的另一個語句。

另一種解決方案,雖然慢了第一個:

 List <String> file1Words = Arrays.asList(words); 
    List <String> file2Words = Arrays.asList(list); 

    for(String s : file1Words) 
     if(!file2Words.contains(s)) 
      System.out.println(s); 

您使用方法Arrays.asList轉換您的數組列表,並使用方法包含覈實,如果第一個文件的話是對的第二個文件。

+0

謝謝我現在有一個可行的解決方案,但我想知道是否有什麼方法可以讓這個過程變得非常快速?有沒有另外一種方法可以比較文件,使其更快? – user1816464

+0

@ user1816464爲了您想要的目的,您的第一個解決方案足夠體面。 – dreamcrash

0

這看起來有點接近。你正在做的是爲words中的每個字符串,你將它與list中的每個單詞進行比較,因此如果在list中有一個字符串不在words中,則x正在設置。

我建議改變if(!words[i].equals(list[j]))if(words[i].equals(list[j]))。所以現在你知道words中的字符串出現在list中,所以你不需要顯示它。如果你完全通過list循環而沒有看到這個詞,那麼你知道你需要解釋它。所以像這樣:

for(int i = 0; i<words.length; i++) 
{ 
    boolean wordFoundInList = false; 

    for(int j = 0; j<list.length; j++) 
    { 
      if(words[i].equals(list[j])) 
      { 
        wordFoundInList = true; 
        break; 
      } 
    } 

    if (!wordFoundInList) { 
     System.out.println(x); 
    } 
} 
+0

謝謝,這就像我一直在尋找,但我試圖讓它儘快比較它是否有任何方式可以更快或有一些不同的方式來儘可能快地做到這一點? – user1816464

+0

不是我所知道的。在一天結束時,你將不得不循環所有內容,直到你檢查了所有內容,或者你已經發現一個字符串在兩個列表中。在這種情況下,你可以跳出循環,這就是我所做的。 – JosephRT

+0

嘿,這就是C#出來了,謝謝你的收穫。 – JosephRT

0

你也可以通過循環,並在你到達list.length-1時添加它。 如果它匹配你可以打破整個東西

FileIO reader = new FileIO(); 
String words[] = reader.load("C:\\list1.txt"); 
String list[] = reader.load("C:\\list2.txt"); 

mergeSort(words); 
mergeSort(list); 
//never ever null 
String x = "" ; 

for(int i = 0; i<words.length; i++) 
{ 
    for(int j = 0; j<list.length; j++) 
    { 
      if(words[i].equals(list[j])) 
       break; 
      if(j == list.length-1) 
       x += words[i] + " "; 
    } 
} 

System.out.println(x); 
0

這裏是一個版本(雖然它不使用排序)

String[] file1 = {"word1", "word2", "word3", "word4"}; 
    String[] file2 = {"word2", "word3"}; 
    List<String> l1 = new ArrayList(Arrays.asList(file1)); 
    List<String> l2 = Arrays.asList(file2); 
    l1.removeAll(l2); 
    System.out.println("Not in file2 " + l1); 

它打印

Not in file2 [word1, word4] 
1

爲什麼不只是轉換數組設置?然後你可以簡單地做 result = wordsSet.removeAll(listSet);

您的結果將包含list2中不存在的所有單詞。TXT

也請記住,該集將刪除重複;)