recursion, which refers to the phenomenon of invoking itself within the current method
Public void method () { System.out.println ("recursive demo"); // call yourself within the current method method ();}
Recursion is divided into two types, direct recursion and indirect recursion.
Direct recursion is called the method itself calling itself. The indirect recursion can be called a method, the B method calls the C method, and the C method calls the A method.
Calculating the and recursive methods between 1-n
Public classDiguidemo { Public Static voidMain (string[] args) {//computes the 1~num and uses recursion to complete intn = 5; intsum =getsum (n); SYSTEM.OUT.PRINTLN (sum); } Public Static intGetsum (intN) {if(n = = 1){ return1; } returnn + getsum (n-1); }}
Note: Recursion must have conditional qualification to ensure that recursion can be stopped, or a stack memory overflow will occur.
Although there are qualifications in recursion, the number of recursion cannot be too many. Otherwise, a stack memory overflow will occur.
Graphic
Recursively print file paths in all subdirectories
Write a method to print the file path in the specified directory and make a call to the method
Requirements: If the specified directory has subdirectories, then the file path in the directory is also printed
Steps:
1. Specify the directory that you want to print the file object
2. Call the Getfileall () method
3. Get all the file objects in the specified directory
4. Iterate to get each file object
5. Determine if the current file object is a directory
The judgment result is true, indicating that the Getfileall () method of step 2 is called by the directory, recursively, by recursion
The result is false, indicating that the file is the path of the print file
Public classFileDemo2 { Public Static voidMain (string[] args) {File file=NewFile ("D:\\test"); Getfileall (file); } //gets all the files in the specified directory and subdirectories Public Static voidgetfileall (file file) {file[] files=File.listfiles (); //traverse all files and folders in the current directory for(File f:files) {//determines whether the current traversal is a directory if(F.isdirectory ()) {//is the directory, continue to get all the files and folders under this directoryGetfileall (f); }Else{ //not a directory, stating that the current F is a file, then print it outSystem.out.println (f); } } }}
Search for. java files in the specified directory (with subdirectories)
Requirement: Prints the file path of the. java file in the specified directory, which is all subdirectories.
Requirement: Write a method to print the path to the. java file in the specified directory and make a call to the method
If the specified directory has subdirectories, the path to the. java file in the handle directory is also printed
Steps:
1. Specify the directory that you want to print the file object
2. Call the Getfileall () method to pass in the directory that you want to print the file object
2.1 Gets all the. Java type file objects in the specified directory through the FilenameFilter filter.
2.2 Traversal to get each file object
2.3 Determine if the current file object is a directory
The judgment result is true, indicating that the Getfileall () method of step 2 is called by the directory, recursively, by recursion
The result is false, indicating that the file is the path of the print file
Public classFileDemo4 { Public Static voidMain (string[] args) {File file=NewFile ("D:\\test"); Getfileall (file); } //gets all the files in the specified directory and subdirectories Public Static voidgetfileall (file file) {file[] files=File.listfiles (Myfilefilter ()); //traverse all files and folders in the current directory for(File f:files) {//determines whether the current traversal is a directory if(F.isdirectory ()) {//is the directory, continue to get all the files and folders under this directoryGetfileall (f); }Else{ //not a directory, stating that the current F is a file, then print it outSystem.out.println (f); } } }}
// Define class implementation file name FilenameFilter filter class Implements filenamefilter{ publicboolean Accept (File dir, String name) { return Name.endswith (". Java");} }
Loops and recursion
For a for or while loop, just take the repeating step with the loop to process, after the loop processing one step, and then the next similar step, the key is to find the basis of the loop and the same point of the steps (the part of the loop), under what circumstances to continue the loop, under what circumstances to terminate the loop, in this case, The number of integers from 1 to max is repeated many times, which is the circular basis for this example, and the repetition of the loop in this example is cumulative;
When using recursion, you first find the same logic between the repeating steps, and then implement the logic in a method.
Recursion is a step that is carried out to a point where it is layered by calling itself, and the next step in the next layer begins,
If the next layer is not the last layer defined, it will be layered again in the same place until the bottom is reached, as in the previous example, if Max
At 5, when you perform this layer of Max 5, you will run Addbymax (4), layered, and max at the next layer will be 4, because Max is defined as 1 o'clock
is the lowest level, so will continue layering, when Max is 3, then points, 2, then points, 1, to the bottom, and then
From the bottom up, the results of each layer are passed up one at a time, returning to the topmost level, returning the final result
Therefore, the recursive should pay attention to the formation of a layer of structure, will generally take a certain method of judgment, such as if, if not meet the requirements
, then call itself recursive, at this time should pay attention to find the upper and lower layers of the connection (the same logical part of each layer), let the operation into the next layer,
This forms the next layer of the call, such as Max+addbymax (MAX-1) in this example
Finally, note the definition of the lowest level, that is, the layer that begins to return the result, and the bottom end will not call itself recursively
java-Basics-Recursion