Java Basics: Recursive application---traversing file directories

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.