I was writing a function in java which counts the number of characters after white spaces in a string. This problem might sound trivial to some of you.
public int countAfterSpaces(final String a){
int position = 0; // escapes leading whitespaces
while(position
Now I want to reuse this variable (position) in a for loop without creating a new one (i) in the initialization statement. Currently I am doing this.
int count = 0;
for (int i=position; i
return count;
}
解决方案
You don't need to declare a new variable:
for (; position
You can leave any field of the for loop blank.
Or better yet, why not:
count = a.length - position;