我对这段代码有一些疑问
public static String reverseString( String s )
{
if ( s.length() == 0 )
return "";
String firstChar = s.substring( 0, 1 );
String reverseRest = reverseString( s.substring( 1 ) );
String result = reverseRest + firstChar;
return result;
}
public static void main( String[] args )
{
String a = "The sky's the limit!";
System.out.println( reverseString( a ) );
}
Run Code Online (Sandbox Code Playgroud)
问题1:终止情况究竟如何运作?s.length如何等于0?
问题2:为什么代码需要"firstChar"能够反转字符串?当reverseString接收0的子字符串而不必添加第一个字符时,为什么代码不起作用?