2016-12-02 62 views
-3

在我的Android應用我有這樣初始化的String[]檢查字符串出現了多少次在String []

String[] array = {"abc", "def", "abc", "ghi", "def", "def"}; 

如何檢查每一個字符串出現了多少次在String[]?例如,​​出現2次,"def"出現3次,而"ghi"只出現一次。

如果你能幫我弄明白的話,我會給你解答的。謝謝!

+3

見http://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array –

+4

我建議你嘗試做先做自己的功課。 –

回答

1

檢查:

public int checkOccurrence(String word, String [] array){ 

    int count=0; 

    for (int i=0; i<array.length; i++){ 

     if(word.equals(array[i])) 
      count++; 
    } 

    return count; 
} 
相關問題