例如,您发送一个字符串“哟怎么了?函数返回4个计数。计算没有任何循环的字符串中的单词数量
下面是我写的:
public class CountString {
public int count(String str){
int count=0;
for(int i=0; i<str.length();i++){
if(str.charAt(i) == ' ')
count++;
}
System.out.printf("\n number of strings: "+ (count+1));
return count+1;
}
}
是否有任何其他方法更快?现在,如何在不包括任何循环的情况下执行相同的工作(for,while,do-while)?
您可以调用其他库方法来为您做,但这些方法包含循环。这是不可能的,去了一个没有循环的前期未知长度的东西。 –