2017-05-21 14 views
-3

私は配列リストを持っています、私はjavaのリスト内の文字列の出現数を探したいと思います。配列リストの文字列の出現数を調べる

と仮定配列リストは、私は非常に明示的ではありません各出現

+1

final String input = "word word test word"; // Splits at word boundary final String[] words = input.split("\\b"); final HashMap<String, Integer> wordToCount = new HashMap<>(); // Iterate all words for (final String word : words) { if (!wordToCount.contains(word)) { // Word seen for the first time wordToCount.put(word, 1); } else { // Word was already seen before, increase the counter final int currentCounter = wordToCount.get(word); wordToCount.put(word, currentCounter + 1); } } // Output the word occurences for (final Entry<String, Integer> entry : wordToCount.entrySet()) { System.out.println("Word: " + entry.getKey() + ", #: " + entry.getValue()); } 

このスニペットの出力は次のようになりますか?それぞれの文字列とその出現数をリンクする 'HashMap'であるか、文字列を受け取り、単一の' int'を返す関数にしますか? – ZeBirdeh

+2

期待される出力は何ですか?各要素はそのリストに一度しか入っていません –

+0

いくつかのコードは、あなたと一緒に解決策を見つけるのに本当に役立ちます。 – steven

答えて

1

あなたの質問の数を取得したい

good girl, good boy, very good girl, she is good, unwanted group, unwanted list

ています。期待される成果とは何か?あなたは、各リストの出現をそれ自身またはすべて一緒に計算しますか?

通常、発生数をカウントするには、Map秒を使用します。 HashMapのように、高速アクセスを可能にします。

ここ

が与えられたテキストのすべての単語の出現箇所を数える小さなスニペットです:あなたは出力がする何をしたいですか

Word: word, #: 3 
Word: test, #: 1 
関連する問題