次の Java のコードは、再帰を使用して、文字列から可能なすべての部分文字列を作成します。これをコーディングするより良い方法があるのだろうか?再帰を使いたい。
public class main {
public static void main(String[] args) {
generate("hello");
}
public static void generate(String word) {
if (word.length() == 1) {
System.out.println(word);
return;
}else{
System.out.println(word);
generate(word.substring(0, word.length()-1));
generate(word.substring(1, word.length()));
}
}
}
FAQ Q - 再帰を使用してこれを行いたいのはなぜですか? A - StackOverflow の CEO が再帰が重要だと言っているため http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html