Introduce recursion in Java and code demonstration, such as recursion factorial, recursion sum, recursion binary number, recursion through file directory and so on.
1 overview of recursion
The phenomenon of calling the method itself in a method definition is called recursion.
Use note:
- Constructors cannot be used recursively.
- Define the end condition, otherwise it is an infinite loop call.
- Avoid running out of memory. Too deep recursion is easy to run out of memory.
2 cases
2.1 Find the factorial of 5
The idea of recursive problem solving:
Code for recursively solving problems:
Recursive code:
public static void main(String[] args) { int factorial = factorial(5); System.out.println(factorial); Num} / * * * * ((num - 1) * ((num - 1-1) * ((num - 1-1-1) *.....................) )) * 5*((5-1)*((5-1-1)*((5-1-1-1)*(5-1-1-1-1)))) * * @param num * @return */ private static int factorial(int num) { If (num > 1) {return num * factorial(num - 1); } else {//num is less than or equal to 1, return 1; }}Copy the code
2.2 to 1 + 2 + 3 +… Num and
public static void main(String[] args) { int sum = getSum(5); System.out.println(sum); } private static int getSum(num) {if (num > 1) {return num + getSum(num-1); } else {//num is less than or equal to 1, return 1; }}Copy the code
2.3 Find the binary of a positive integer num
public static void main(String[] args) { String binary = getBinary(9); System.out.println(binary); } private static String getBinary(int num) {if (num / 2! = 0); = 0) { return "" + getBinary(num / 2) + num % 2; } else {/// otherwise it is the last calculation, return "" + num % 2; }}Copy the code
2.4 Print the value of the NTH Fibonacci sequence
1, 1, 2, 3, 5, 8, 13, 21, 34… // The latter term is the sum of the first two terms
public static void main(String[] args) { long value = getValue(8); System.out.println(value); } private static long getValue (int n) {/ / if n is 1 or 2, the direct return 1 if (n = = 1 | | n = = 2) {return 1; } return getValue(n-1) + getValue(n-2);} return getValue(n-2); }Copy the code
2.5 Deleting a File directory
Windows, the principle of deleting a directory:
- Recurse to the bottom of the directory.
- Delete the existing files in the directory first.
- Then delete the empty directory.
Summary: Delete from the inside out, the directory to be deleted should not be occupied.
Note: this deleted files can not be found in the recycle bin, do not delete important files.
Public static void main(String[] args) {delete("C:\Users\lx\Desktop\test"); } static void delete(String path) { File file = new File(path); if (file.exists()) { if (file.isFile()) { file.delete(); return; } File[] files = file.listFiles(); for (File file1 : files) { if (file1.isFile()) { file1.delete(); } else { delete(file1.getPath()); } } file.delete(); }}Copy the code
2.5 File List
List all files under a folder into a directory into another TXT file
Public static void main(String[] args) {// output to makeList("J:\Idea\JavaSE", "C:\Users\lx\Desktop\test.txt"); } private static void makeList(String source, @param place, @param place, @param place, @param place, @param place, @param place, @param place) String place) { ArrayList<String> al = new ArrayList<>(); File file = new File(source); File[] files = file.listFiles(); assert files ! = null; for (File file1 : files) { if (file1.isFile()) { al.add(file1.getAbsolutePath()); } else {// recursively iterate through all Java files makeList(file1.getabSolutePath (), place); } } if (al.isEmpty()) { return; } System.out.println(); System. The out. Println (" folder: "+ file. GetAbsolutePath () +" document traversal start = = = = = = = = = = = = = = = > "); Try (BufferedWriter bw = new BufferedWriter(new FileWriter(place, true))) {for (String s: al) {bw.write(s); bw.newLine(); bw.flush(); System.out.println(s); } System. Out. Println (" folder: "+ file. GetAbsolutePath () +" file traversal = = = = = = = = = = = = = = = > "); System.out.println(); } catch (IOException e) { e.printStackTrace(); }}Copy the code
Demonstration of output results:
文件夹 :J:\Idea\JavaSE.idea\inspectionProfiles 文件遍历开始===============>
J:\Idea\JavaSE.idea\inspectionProfiles\Project_Default.xml
文件夹 :J:\Idea\JavaSE.idea\inspectionProfiles文件遍历结束===============>
文件夹 :J:\Idea\JavaSE.idea 文件遍历开始===============>
J:\Idea\JavaSE.idea\compiler.xml
J:\Idea\JavaSE.idea\misc.xml
J:\Idea\JavaSE.idea\uiDesigner.xml
J:\Idea\JavaSE.idea\workspace.xml
文件夹 :J:\Idea\JavaSE.idea文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\annocation 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\annocation\ManAnnotation.java
J:\Idea\JavaSE\src\main\java\com\javase\annocation\Student.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\annocation文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\enums 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\enums\Direction.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\Direction2.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\Direction3.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\DirectionEnum1.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\DirectionEnum2.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\DirectionEnum3.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\DirectionTest.java
J:\Idea\JavaSE\src\main\java\com\javase\enums\EnumApi.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\enums文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\genericity 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\genericity\GenericDemo01.java
J:\Idea\JavaSE\src\main\java\com\javase\genericity\Genericity.java
J:\Idea\JavaSE\src\main\java\com\javase\genericity\ss.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\genericity文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\innerclass 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\innerclass\Outer1.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\innerclass文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\recursion 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\recursion\RecursionTest.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\recursion文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\reflect 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\reflect\ArrayListDemo.java
J:\Idea\JavaSE\src\main\java\com\javase\reflect\Student.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase\reflect文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase 文件遍历开始===============>
J:\Idea\JavaSE\src\main\java\com\javase\A.java
J:\Idea\JavaSE\src\main\java\com\javase\BaseTest.java
J:\Idea\JavaSE\src\main\java\com\javase\Operator.java
J:\Idea\JavaSE\src\main\java\com\javase\PerpetualCalendar.java
J:\Idea\JavaSE\src\main\java\com\javase\Teacher.java
文件夹 :J:\Idea\JavaSE\src\main\java\com\javase文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\annocation 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\annocation\ManAnnotation.class
J:\Idea\JavaSE\target\classes\com\javase\annocation\Student.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\annocation文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\enums 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction.class
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction2.class
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction3$1.class
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction3$2.class
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction3$3.class
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction3$4.class
J:\Idea\JavaSE\target\classes\com\javase\enums\Direction3.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum1.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum2.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum3$1.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum3$2.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum3$3.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum3$4.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionEnum3.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionTest$1.class
J:\Idea\JavaSE\target\classes\com\javase\enums\DirectionTest.class
J:\Idea\JavaSE\target\classes\com\javase\enums\EnumApi.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\enums文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\genericity 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\genericity\GenericDemo01.class
J:\Idea\JavaSE\target\classes\com\javase\genericity\Genericity.class
J:\Idea\JavaSE\target\classes\com\javase\genericity\ss.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\genericity文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\innerclass 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\innerclass\Outer1$1Inner3.class
J:\Idea\JavaSE\target\classes\com\javase\innerclass\Outer1$Inner1.class
J:\Idea\JavaSE\target\classes\com\javase\innerclass\Outer1$Inner2.class
J:\Idea\JavaSE\target\classes\com\javase\innerclass\Outer1.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\innerclass文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\recursion 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\recursion\RecursionTest.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\recursion文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\reflect 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\reflect\ArrayListDemo.class
J:\Idea\JavaSE\target\classes\com\javase\reflect\Student.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase\reflect文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\com\javase 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\com\javase\A$B.class
J:\Idea\JavaSE\target\classes\com\javase\A.class
J:\Idea\JavaSE\target\classes\com\javase\BaseTest.class
J:\Idea\JavaSE\target\classes\com\javase\Operator.class
J:\Idea\JavaSE\target\classes\com\javase\PerpetualCalendar.class
J:\Idea\JavaSE\target\classes\com\javase\Teacher$Student.class
J:\Idea\JavaSE\target\classes\com\javase\Teacher.class
文件夹 :J:\Idea\JavaSE\target\classes\com\javase文件遍历结束===============>
文件夹 :J:\Idea\JavaSE\target\classes\META-INF 文件遍历开始===============>
J:\Idea\JavaSE\target\classes\META-INF\JavaSE.kotlin_module
文件夹 :J:\Idea\JavaSE\target\classes\META-INF文件遍历结束===============>
文件夹 :J:\Idea\JavaSE 文件遍历开始===============>
J:\Idea\JavaSE\pom.xml
文件夹 :J:\Idea\JavaSE文件遍历结束===============>
Copy the code
Of course, you can also choose to iterate over files at the end of a certain category, such as.java, and you just need to add.java to the filter criteria, so you can achieve a more precise search.
If you need to communicate, or the article is wrong, please leave a message directly. Also hope to like, collect, follow, I will continue to update a variety of Java learning blog!