呜呜,自己好不容易写出来个递归,却解释不清楚,谁能解释下。
public class Test3T5 {
public static final ArrayList<StringBuilder> ARRAY_LIST=new ArrayList<>();
public static void resort(String S){
char[] chars=S.toCharArray() ;
StringBuilder sb =new StringBuilder() ;
for(int i=0;i<chars.length;i++){
if(chars[i]==' '){
//更新S,参与递归
S=S.substring(i+1);
resort(S);
//最后一次递归调用resort结束条件是for循环结束
//其他结束全靠这个break
break ;
}
sb.append(chars[i]);
}
ARRAY_LIST.add(sb);
}
public static void main(String[] args) {
String S ="hello world Java 牛叉 !" ;
resort(S);
for(StringBuilder i:ARRAY_LIST){
System.out.print(i+" ");
}
}
}