2014-02-17 67 views
1

結果如何在不使用集合的情況下計算單詞的頻率?

0: Bear 
1: Car 
2: Bear 
3: Cat 
4: Car 
5: Dog 
6: Bear 
---Frequency--- 
Bear : 1 
Car : 1 
null : 1 
Cat : 1 
null : 1 
Dog : 1 
null : 1 

代碼

import java.util.Arrays; 
import java.util.StringTokenizer; 

public class WordCount { 

    public static void main(String[] args) { 

     String text = "Bear Car Bear Cat Car Dog Bear"; 
     StringTokenizer str = new StringTokenizer(text); 
     String word[] = new String[10]; 
     String unique[] = new String[10]; 
     String w; 

     int count = -1; 

     while (str.hasMoreTokens()) { 
      count++; 
      w = str.nextToken(); 
      word[count] = w; 

      System.out.println(count + ": " + word[count]); 

     } 

     System.out.println("---Frequency---"); 

     // create unique words 
     for (int i = 0; i < 7; i++) { 

      if ((!Arrays.asList(unique).contains(word[i]))) { 
       unique[i] = word[i]; 
      } 
     } 

     // measuring frequency 
     int[] measure = new int[10]; 

     for (int z = 0; z < 7; z++) { 
      if (Arrays.asList(unique).contains(word[z])) { 
       measure[z] += 1; 
       System.out.println(unique[z] + " : " + measure[z]); 
      } 
     } 
    } 
} 
+0

是一個不是集合的數組? –

+0

爲什麼你要使用這種方法,而不是使用'Map '? –

+0

我編輯了我的問題。我想,爲了這樣做。 –

回答

2

有幾個問題在當前的代碼:

  1. 您使用Collection接口明確。每當您撥打Arrays#asList(...)時都會完成此操作。你沒有滿足你的主要要求。
  2. 您應該爲所有數組中的元素維護一個計數器。在這種情況下,您可以使用相同的變量來保存uniquemeasure陣列的大小。
  3. 您的算法填寫unique是錯誤的。您只應添加一次(因爲它必須是唯一的)。
  4. 您應該在unique陣列中每String只添加一次measure的計數器。

此代碼考慮了所有這些建議。

import java.util.StringTokenizer; 

public class ThirteenthMain { 

    public static void main(String[] args) { 

     String text = "Bear Car Bear Cat Car Dog Bear"; 
     StringTokenizer str = new StringTokenizer(text); 
     String word[] = new String[10]; 
     String unique[] = new String[10]; 
     // reading the words to analyze 
     int wordSize = 0; 
     while (str.hasMoreTokens()) { 
      String w = str.nextToken(); 
      word[wordSize] = w; 
      System.out.println(wordSize + ": " + word[wordSize]); 
      wordSize++; 
     } 
     System.out.println("---Frequency---"); 
     // create unique words 
     int uniqueWordSize = 0; 
     for (int i = 0; i < wordSize; i++) { 
      boolean found = false; 
      for (int j = 0; j < uniqueWordSize; j++) { 
       if (word[i].equals(unique[j])) { 
        found = true; 
        break; 
       } 
      } 
      if (!found) { 
       unique[uniqueWordSize++] = word[i]; 
      } 
     } 
     // measuring frequency 
     int[] measure = new int[10]; 
     for (int i = 0; i < uniqueWordSize; i++) { 
      for (int j = 0; j < wordSize; j++) { 
       if (unique[i].equals(word[j])) { 
        measure[i]++; 
       } 
      } 
     } 
     //printing results 
     for (int i = 0; i < uniqueWordSize; i++) { 
      System.out.println(unique[i] + " : " + measure[i]); 
     } 
    } 
} 

它打印:

0: Bear 
1: Car 
2: Bear 
3: Cat 
4: Car 
5: Dog 
6: Bear 
---Frequency--- 
Bear : 3 
Car : 2 
Cat : 1 
Dog : 1 
+0

嘿Luiggi,謝謝你的回答。在意識到(從您的答案中得到啓發)之後,我編輯了自己的代碼,我已經臭名昭着地錯過了最後的嵌套循環。我只需對現有代碼進行幾行更改即可完成工作。 –

1

感謝Luiggi爲靈感。這是我的解決方案,意識到我錯過了一件非常重要的事情。嵌套循環。這只是我現有代碼的幾行編輯。我希望大家都能看到Luiggi的代碼,因爲它比較冗長(hehe)。

結果

0: Bear 
1: Car 
2: Bear 
3: Cat 
4: Car 
5: Dog 
6: Bear 
---Frequency--- 
Bear : 3 
Car : 2 
Cat : 1 
Dog : 1 

import java.util.Arrays; 
import java.util.StringTokenizer; 

public class WordCount { 

    public static void main(String[] args) { 

     String text = "Bear Car Bear Cat Car Dog Bear"; 
     StringTokenizer str = new StringTokenizer(text); 
     String word[] = new String[10]; 
     String unique[] = new String[10]; 
     String w; 

     int count = -1; 

     while (str.hasMoreTokens()) { 
      count++; 
      w = str.nextToken(); 
      word[count] = w; 

      System.out.println(count + ": " + word[count]); 

     } 

     System.out.println("---Frequency---"); 

     // create unique words 
     for (int i = 0; i < 7; i++) { 

      if ((!Arrays.asList(unique).contains(word[i]))) { 

       unique[i] = word[i]; 
      } 

     } 

     // measuring frequency 
     int[] measure = new int[10]; 

     for (int z = 0; z < 7; z++) { 

      if (unique[z] != null) { 

       for (int j = 0; j < 7; j++) { 

        if (unique[z].equals(word[j])) { 

         measure[z] += 1; 

        } 
       } 
       System.out.println(unique[z] + " : " + measure[z]); 
      } 

     } 

    } 

} 
+1

好的,它的工作原理:) –

+0

是的,它的確如此。 :) –

1

另一種解決方案:

String text = "Bear Car Bear Cat Car Dog Bear"; 
String[] allWords = text.split(" "); 
String[] foundWords = new String[allWords.length]; 
int[] foundCount = new int[allWords.length]; 
int foundIndex= 0; 

for (String aWord : allWords) { 
    int j = 0; 
    for (; j < foundIndex; j++) { 
     if (foundWords[j].equals(aWord)) { //found 
      foundCount[j]++; 
      break; 
     } 
    } 
    if (j == foundIndex) { //word bot found in foundWords 
     foundWords[foundIndex] = aWord; 
     foundCount[foundIndex] = 1; 
     foundIndex++; 
    } 
} 

// Print result 
for (int i = 0; i <foundIndex ; i++) { 
    System.out.println(foundWords[i] + " : " + foundCount[i]); 
} 

結果是:

Bear : 3 
Car : 2 
Cat : 1 
Dog : 1 
+0

非常好先生阿斯霍特。 –

+0

謝謝,這對我來說很有趣:) –

+0

+1,但是我發現錯誤後已經編輯了我的代碼。檢查:) –

相關問題