Keywords: Java

# Java implementation of recursion and classic cases

This paper briefly introduces the concept of recursion and points for attention when using recursion, and shares the cases of factorial case (two ways), immortal rabbit case (three ways) and deleting a file with content by recursion;

# 1. Introduction to recursion

Recursion is a widely applied algorithm. In practical development, it can solve specific problems with specific rules. When problems need to be solved, it can be split into several small problems. The same size problem solving method or regular rule is called the method itself in the method definition. All recurring problems can be recursively solved. To solve;

Precautions for using recursion:

• There should be exits, or dead recursion
• The number of times cannot be too many, or the memory will overflow
• Constructor cannot be used recursively
• There are certain rules
• The idea of solving problems recursively is to find out the rules of mouth

# 2. Factorial case

Factorial of 5

• n!=n*(n-1)!..

## 2.1 circulation mode

```public static void main(String[] args) {
int jc = 1;
for(int i = 1; i <= 5; i++){
jc*=i;
}
System.out.println("5 The factorials of are:" + jc);//The factorial of 5 is: 120
}
```

## 2.2 recursive mode

```public static void main(String[] args) {
System.out.println("5 The factorials of are:" +jiecheng(5));//The factorial of 5 is: 120
}

private static int jiecheng(int i) {
if(i == 1){
return 1;
}else{
return i*jiecheng(i-1);
}
}
```

# 3. The case of immortal rabbit

Requirements:

• There is a pair of rabbits. From the third month after birth, a pair of rabbits will be born each month. From the third month after birth, a pair of rabbits will be born each month. If the rabbits are not dead, how many pairs of rabbits are there in the twentieth month?

law:

• The first and second month's rabbit logarithm is 1. From the beginning of the third month, the data value itself is equal to the sum of the first two items;

Principle:

• Fibonacci series

## 3.1 array mode

Ideas:

• Define an array of length 20: int[] arr = new int;
• The logarithm of the first and second months is 1: so the values of indexes 0 and 1 are 1:
• arr = 1;
• arr = 1;
• The logarithm of the next month from the third month is the sum of the logarithm of the month: arr = arr+arr;
```public static void main(String[] args) {
//Define an array of 20 months
int[] arr = new int;
//1 for the first two months
arr = 1;
arr = 1;

//The logarithm of the next month from the third month is the sum of the logarithm of this month
for(int i = 2; i < arr.length; i++){
arr[i] = arr[i - 1] + arr[i-2];
}
System.out.println("The number of rabbits in the 20th month is:" + arr);//The number of rabbits in the 20th month is 6765
}
```

## 3.2 change mode of variables

Ideas:

• Two variables a and b are defined to represent the logarithm of the rabbits in the next two months
• First month, second month: a = 1, B = 1
• Second month, third month: a = 1, B = 2
• The third month, the fourth month: a=2, b=3
• It can be seen from the above that the next a is the value of the last b, and the next b is the value of the last a+b
```public static void main(String[] args) {
//First two months known
int a = 1;
int b = 1;

//For loop traversal
for(int i = 0; i <18; i++){
int temp = a;
a = b;//a value of next month
b = temp +a;//b value of next month
}
System.out.println("The number of rabbits in the 20th month is:" + b);//The number of rabbits in the 20th month is 6765
}
```

## 3.3 recursive mode

Ideas:

• Define a method
• Export conditions
• End when i equals 1 or 2
• law
• If it is not the first month and the second month, the number of rabbits in each month is equal to the sum of the first two months: method name (n-1) + method name (n-2);
```public static void main(String[] args) {
System.out.println("The number of rabbits in the 20th month is:" + get(20));//The number of rabbits in the 20th month is 6765
}

private static int get(int i) {
if(i == 1 || i == 2){//Export conditions
return 1;
}else{
return get(i - 1) + get(i - 2);//law
}
}
```

# 4. Use recursion to delete a file case with content

Requirements:

• There is a folder named kaka under disk D. There are several subfolders and files in it. You need to delete all the items in this folder

Ideas:

• Encapsulating the kaka folder File class
• Define a recursive method, deletsrcfolder (file file file)
• In the recursive method, first get all the files of the current directory and the File array of the folder
• If it is not null, traverse the File array to get each object
• Determine whether the current File object is a folder (directory) isDirectory()
• If you want to go back to the previous step and execute again to get all the files in the current folder and the File array of the folder
• If it's not a deleted file, get the name
```public static void main(String[] args) {
//Create File object
File fileSrc = new File("d:\\kaka") ;
//Define how to delete a folder
deleteSrcFolder(fileSrc) ;
}

private static void deleteSrcFolder(File srcFolder) {
//Get all files under the srcFolder folder and the File array of the folder
File[] fileArray = srcFolder.listFiles() ;
//Judge if it is not null, and then traverse
if(fileArray !=null) {
for(File file : fileArray) {
//Determine whether the File object is a folder
if(file.isDirectory()) {
//Call recursive delete method
deleteSrcFolder(file) ;
}else {
//Not a folder
System.out.println(file.getName()+"---"+file.delete());
}
}
System.out.println(srcFolder.getName()+"---"+srcFolder.delete()); //delete() can only delete empty directories
}
}
```

Posted by jestaji on Thu, 25 Jun 2020 00:19:46 -0700