2013-10-04 51 views
0

我只想獲取有關刪除重複項的幫助。到目前爲止,我有這個,但它不會刪除或刪除多次出現的單詞。如何從Java中的字符串中刪除重複的字符

void antUnikOrd() { 
int unikCount = 0; 
String c = "ahfuwa"; 
for(int i = 0; i<a.length;i++) { 


    for(int j= 0;j<a.length;j++) { 
    if(a[i].equals(a[j])) {   
     unikCount++; 
    } 
    if(unikCount>1) { 
     a[j] = c; 
     unikCount = 1; 

    }  
     unikCount = 0;    
    } 

    for(i = 0;i<a.length;i++) { 
    //if(a[i] != " ") { 
    System.out.println(a[i]); 
    // } 
    } 
} 
+0

您期待從內存中刪除它們以從文件中刪除它們嗎? –

+0

您是否希望我們爲您編寫該功能?或者您有關於您的特定嘗試的任何問題? – Oswald

+0

人們傾向於使用正則表達式來滿足這些要求。 – Rorschach

回答

0

您可以存儲元素融入HashSet的自動刪除重複

+0

擴大這個答案,將其更具體地鏈接到這個問題。操作系統的問題是寫回文件。 – christopher

+0

我們不允許使用哈希集,地圖或陣列列表。所以我需要使用這個。我在正確的軌道上嗎? –

0

您可以添加字符串到HashSet的,它會刪除重複項。

+0

void lesFraFil(){ \t Scanner sc = new Scanner(System。在); \t int count; \t \t System.out.println(「Gi navnet til filen:」); \t String filNavn = sc.next(); \t嘗試{ \t \t文件k =新文件(filNavn); \t \t掃描儀sc2 =新的掃描儀(k); \t \t count = 0; \t \t while(sc2.hasNext()){ \t \t count ++; \t \t sc2.next(); \t \t} \t \t Scanner sc3 = new Scanner(k); \t \t a = new String [count]; \t \t tempAry = new String [count]; (int i = 0; i

+0

好吧,你是什麼意思的代碼是什麼意思? – slanecek

+0

它是我的代碼的開始加上我上面寫的代碼 –

0

當您替換文件中的文本時,我經常會將整個文件讀入內存,執行任何我想要執行的操作,然後將其全部寫回到文件中。我不喜歡給出答案,所以我會給你類似的東西。例如,在僞代碼:

public void removeWord(String word) 
{ 
    fileReader := FileReader(the file to read) 
    lines := Java HashSet object 
    for every line in the file { 
     // Cycle through each line and load into the HashSet 
     lines.add(current line) 
    } 

    // You now have a whole bunch of different lines. 

    fileReader.close(); 
    // Unlock the file. 

    fileWriter := FileWriter(the file to write in overwrite mode) 

    for every line in lines 
    { 
     fileWriter.write(line) 
    } 
    fileWriter.flush() // To be safe.. 
    fileWriter.close() // to prevent memory leaks. 

} 
0

的問題是稍微不清楚,但我會假設你想讀取文件的內容,刪除重複和寫回文件。

一旦你的文件的內容(請參閱指導這樣一個問題:Reading a plain text file in Java),然後從列表中刪除重複的最簡單方法是將它們放到一個集:

List<String> lines = readFromFile(); // complete this method 
Set<String> uniqueLines = new HashSet<String>(lines); 

一旦你有一組獨特的線條,你可以簡單地把它們寫回到一個文件中(請參閱這個問題的指導原則:How do I create a file and write to it in Java?

1

如果你不允許使用額外的內存,並且非常方便的Java集合,那麼存在置換算法來做你想要的是O(NlogN)而不是你提出的明顯的O(N^2)解決方案。

1 - Sort the array of words (Arrays.sort(~) will do the trick in O(nlogn)). 
2 - For each word in the sorted array look if the next one is equal. (one loop) 
    a - TRUE = set to delete current word from array (not the next one, keep that one) 
    b - FALSE = go on to next 
3 - Write to file by ignoring the detect duplicates. (one more loop) 

爲了解釋點2:

array = [ a, b, b, c, d, d, d ] 
ITERATIONS 
- a != b -> [ a, b, b, c, d, d, d ] index = 0 
- b == b -> [ a, X, b, c, d, d, d ] index = 1 
- ... 
- d == d -> [ a, X, b, c, X, d, d ] index = 4 
- d == d -> [ a, X, b, c, X, X, d ] index = 5 
- d is last so we stop 

現在我們篩選兩個X:可以被簡化爲O(nlogn)

[a, b, c, d] 

這實際上是O(nlogn + 2N) 。

祝你好運,但它應該相當簡單。 如果您不能使用Arrays.sort(〜)實現您自己的排序功能,我建議您使用QuickSort或MergeSort,因爲它們決定了此解決方案的整體性能。

+0

我們只需要使用,如果用數組來獲得答案。我用平行數組來啓動它。但不能正確地得到它 –

+0

我的解決方案完全使用 –

+0

將檢測到的重複項設置爲空,並在寫回文件時忽略它們?由於它似乎是功課我可以給你這個,但不會把代碼。 –

相關問題