I. Overview
Recursive : it refers to the phenomenon that calls itself in the current method.
Recursive Category :
-
- Recursively divided into two types, direct recursion and recursive Profile
- Direct method calls itself recursively called themselves
- Indirect recursion can be called Method B Method A, B Method C Method calls, C A method of method calls.
- Recursively divided into two types, direct recursion and recursive Profile
Note :
-
- Recursive must be conditional guarantee recursion can be stopped, otherwise it will happen stack memory overflow.
- Although there are defined in a recursive condition, but not too much recursion, Content Gou Ze stack overflow will occur.
- Construction method, binary recursion.
Recursive principle lead to a stack overflow memory:
Second, the case
1, the recursive cumulative sum
1 and calculated, the accumulated n, n = n + (n + 1) and the tired, can be accumulated and defined as a method of operation of a recursive call.
achieve:
. 1 public class Demo {
2 public static void main (String [] args) {
. 3 // calculated and 1 ~ num, recursive completed
. 4 int NUM =. 5 ;
. 5 // method call summed
. 6 int SUM = GetSum ( NUM);
. 7 // output
. 8 System.out.println (SUM);
. 9 }
10 / *
. 11 implemented by a recursive algorithm.
12 list of parameters: int
13 is return value type: int
14 * /
15 public static intGetSum ( int NUM) {
16 / *
. 17 NUM is 1, the method returns 1,
18 corresponds to the export process, where NUM is always 1
. 19 * /
20 is IF (NUM == 1 ) {
21 is return 1 ;
22 is }
23 is / *
24 NUM 1 is not returned, the method num + (num-1) and the accumulated
25 GetSum recursive call
26 is * /
27 return NUM + GetSum (NUM-1 );
28 }
29 }
Code execution illustration :
Note : Recursion limit must be qualified to ensure that recursion can be stopped, the number of not too much, otherwise it will happen stack memory overflow.
2, recursive factorial
Factorial : product of all positive integers less than and equal to the number.
分析:n! = n * (n-1);
实现:
1 public class DiGuiDemo {
2 //计算n的阶乘,使用递归完成
3 public static void main(String[] args) {
4 int n = 3;
5 // 调用求阶乘的方法
6 int value = getValue(n);
7 // 输出结果
8 System.out.println("阶乘为:"+ value);
9 }
10 /*
11 通过递归算法实现.
12 参数列表:int
13 返回值类型: int
14 */
15 public static int getValue(int n) {
16 // 1的阶乘为1
17 if (n == 1) {
18 return 1;
19 }
20 /*
21 n不为1时,方法返回 n! = n*(n‐1)!
22 递归调用getValue方法
23 */
24 return n * getValue(n ‐ 1);
25 }
26 }
3、递归打印多级目录
分析:多级目录的打印,就是当目录的嵌套。遍历之前,无从知道到底有多少级目录,所以我们还是要使用递归实现。
实现:
1 public class DiGuiDemo {
2 public static void main(String[] args) {
3 // 创建File对象
4 File dir = new File("D:\\aaa");
5 // 调用打印目录方法
6 printDir(dir);
7 }
8 public static void printDir(File dir) {
9 // 获取子文件和目录
10 File[] files = dir.listFiles();
11 // 循环打印
12 /*
13 判断:
14 当是文件时,打印绝对路径.
15 当是目录时,继续调用打印目录的方法,形成递归调用.
16 */
17 for (File file : files) {
18 // 判断
19 if (file.isFile()) {
20 // 是文件,输出文件绝对路径
21 System.out.println("文件名:"+ file.getAbsolutePath());
22 } else {
23 // 是目录,输出目录绝对路径
24 System.out.println("目录:"+file.getAbsolutePath());
25 // 继续遍历,调用printDir,形成递归
26 printDir(file);
27 }
28 }
29 }
30 }