[英]How does recursive method to count white spaces in a string work?
I'm trying to fully understand how the method works, see the code below: 我试图全面了解该方法的工作原理,请参见下面的代码:
public static void main(String[] args) {
System.out.println(countspaces("a number of spaces "));
}
public static int countspaces(String s) {
if (s.length() == 0)
return 0;
else
return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));
}
I've debugged the method using BlueJ. 我已经使用BlueJ调试了该方法。 The line:
该行:
return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));
firstly checks if the character at the index zero is a white space, then it calls itself again (this makes it recursive) taking the substring of s starting at index 1 as an argument effectively changing the argument from "a number of spaces " to " number of spaces " and doing it till the argument's length() reaches 0. What I don't get is why it's not returning 01000000100100000010 (the last 0 being for the empty string s which terminates the loop) but 4? 首先检查索引为零的字符是否为空格,然后再次调用自身(这使其递归),以从索引1开始的s的子字符串作为参数,从而有效地将参数从“多个空格”更改为“直到参数的length()达到0为止。我不明白的是为什么它不返回01000000100100000010(最后一个0是用于终止循环的空字符串s)而是4? I can't see where in the code it sums up the 1's returned by
我看不到它在代码中的总和由1返回
(s.charAt(0) == ' ' ? 1 : 0)
and ignoring the 0's. 并忽略0。 Please advise me what is missing from my reasoning.
请告诉我我的推理中缺少的内容。
Many Thanks 非常感谢
Grzegorz(Greg) 格热哥兹(格雷格)
(s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1))
This one basically sums up the 0
s and 1
s. 这个基本上将
0
s和1
s相加。
Take note of the return value of the method which is an int
. 注意该方法的返回值
int
。 The return value of 4
is perfectly fine. 返回值
4
非常好。
To put in other words: 换句话说:
0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 = 4
0 + 1 + 0 + 0 + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 = 4
Since the method returns an int, not a string, it adds the numbers, not concatenates as characters/strings. 由于该方法返回的是int而不是字符串,因此它会添加数字,而不是串联为字符/字符串。 ie
即
0+1+0+0+0+0+0+0+1+0+0+1+0+0+0+0+0+0+1+0 == 4
not 不
"0"+"1"+"0"+"0"+"0"+"0"+"0"+"0"+"1"+"0"+"0"+"1"+"0"+"0"+"0"+"0"+"0"+"0"+"1"+"0"
== "01000000100100000010"
below returns an int, since countspaces returns an int 下面返回一个int,因为countspaces返回一个int
return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));
Here's an attempt to write the function in "English" in case that helps you understand it: 为了帮助您理解它,请尝试用“英语”编写该函数:
int countspaces( string ) {
if string has no characters {
return 0
}
else {
if first character in string is a space
add 1
otherwise
add 0
to result of countspaces( string_with_first_character_removed )
}
}
The point of recursion is that the function is called over and over again, you could roughly say it's some sort of loop. 递归的点是该函数被一遍又一遍地调用,您可以粗略地说这是某种循环。
if (s.length() == 0)
return 0;
This code is stopping condition (because the recursion stops at this point) of your recursive function, it will return 0 when the length of provided string is 0. Nothing much to explain here, I think that is pretty obvious. 这段代码是递归函数的停止条件(因为递归此时已停止),当提供的字符串的长度为0时,它将返回0。这里没有什么要解释的,我认为这很明显。
And this part is core part of your recursive function: 这部分是递归函数的核心部分:
return (s.charAt(0) == ' ' ? 1 : 0) + countspaces(s.substring(1));
s.charAt(0) == ' ' ? 1 : 0
s.charAt(0) == ' ' ? 1 : 0
uses a ternary operator , it checks if the first character in string is space, if it's true it uses value of 1, else it uses 0. s.charAt(0) == ' ' ? 1 : 0
使用三元运算符 ,它检查字符串中的第一个字符是否为空格,如果为true,则使用值1,否则使用0。
countspaces(s.substring(1))
calls the function again, with substring that removes first character. countspaces(s.substring(1))
再次调用该函数,并且子字符串会删除第一个字符。
Function returns int
value of 0 or 1, it sums the values returned, and as you know, x + 0 = x
, so only the cases where first character is space (function returns 1) affect your final result. 函数返回的
int
值为0或1,它对返回的值进行求和,如您所知, x + 0 = x
,所以只有第一个字符为空格(函数返回1)的情况才会影响最终结果。
This calls occurs until the function passes itself empty string, when it hits the stopping condition , where it goes back through the call stack, summing returned values and values from ternary operator and finally returns the expected result. 调用将一直进行到函数传递自身的空字符串(达到停止条件)时 ,才返回调用堆栈,返回的值与三元运算符的值相加,最后返回预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.