【发布时间】:2021-01-28 18:23:20
【问题描述】:
public static void recur(String s)
{
if(s.length() == 0)
return;
else
{
System.out.println(s.charAt(0));
recur(s.substring(1));
}
}
【问题讨论】:
-
为什么会崩溃?你看过文档吗?你为什么会期望它崩溃?
-
"y".substring(1)返回空字符串"",以便将其作为参数传递给另一个递归调用,if(s.length() == 0)将处理它。 -
我认为因为字符串的长度是 1 并且唯一的索引位置是 0,所以 s.substrin(1) 不应该工作并且它会给出一个 StringIndexOutOfBoundsException 因为没有索引 =1。