One: Recursive related knowledge
The programming technique called by the program itself is called recursion (recursion).
A method in which a procedure or function calls itself directly or indirectly in its definition or description, which usually transforms a large complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program. The ability to recursion is to define an infinite set of objects with limited statements. The program written with recursive thought is often very simple and easy to understand.
In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
Attention:
(1) Recursion is the invocation of itself in a process or function;
(2) When using the incremental return strategy, there must be a definite recursive end condition called the recursive exit.
Recursive algorithms are generally used to solve three types of problems:
(1) The definition of the data is defined by recursion. (Fibonacci function)
(2) The problem solution is implemented by recursive algorithm. Back
(3) The structural form of the data is defined by recursion. (Traversal of the tree, search of the graph)
Disadvantages of recursion:
Recursive algorithm is less efficient in solving problems. In the process of recursive invocation, the system opens up a stack for each layer's return point, local quantity and so on. Too many recursion times can cause stack overflow and so on.
Two: Traversing the file directory
Output Result:
But this does not see the structure of the file directory, continue to refine the code:
Output Result:
Java Basics: Recursive application---traversing file directories