非常に長い文字列内のいずれかの単語が「the」という単語であるかどうかを確認する必要があるため (次に、これが何回発生したかを数えます)、文字列をリストの個々の要素に分割します。
String [ ] list1 = phrase1.split(" ");
リストの個々の要素を文字列「the」と比較するにはどうすればよいですか?
私が使用する配列で
int count = 0;
for (int i = 0; i < string.length; i++)
{
if (string[i].equals("the"))
{
count++;
}
}
return count;
ただし、そのようなリストで要素を呼び出すことはできません。私は何をしますか?
リストで equals メソッドを使用できますか?
編集: 次のように受け取った回答を適用しようとしました
static public int countWordsEqualTo( String [ ] list, int wordcount, String wordsearch)
{
int count = 0;
String[] list1 = list.split(" ");
for(String word: list1)
{
if (word.equals(wordsearch))
{
count++;
}
}
return count;
}
コンパイルされません。私は何を間違えましたか?