In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use recursion in Java", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use recursion in Java" this article.
What is recursion?
The programming skill in which a program calls itself is called recursion.
What are the advantages of recursion?
Recursive algorithm: the code is simple, clear, and easy to verify correctness. To a certain extent, it can also help us reduce a lot of repetitive code.
The difference between iteration and Recursion
The iteration is gradually approaching, covering the old value with the new value until the end of the condition is satisfied, the intermediate value is not saved, and the space utilization rate is high.
Recursion is to decompose a problem into a number of relatively smaller problems, encounter recursive exit and then return the original way, so we must save the relevant intermediate values, these intermediate values are pressed into the stack to save, when the problem is large, it will take up a lot of memory.
Three conditions of Recursion
Boundary condition
Recursive forward segment
Recursive return segment
When the boundary condition is not satisfied, the recursive forward; when the boundary condition is satisfied, the recursive return.
In what scenario is it suitable to use recursive scenario one
Many menus in the project are configured, and sometimes the menus are divided into several levels, when I configure him with the lowest level, then I have to save his superiors before I can use them, but we are not sure how many superiors he has. Recursive calls can be used at this time.
Public void packageParent (Set parentIdSet) {Set parentIdSet1 = new HashSet (); for (String parentId: parentIdSet) {MenuOrg menuOrg = new MenuOrg (); Menu menu = menuRepository.findOne (parentId); if (menu = = null) {continue;} menuOrg.setMenuId (menu.getMenuId ()); menuOrg.setProType (menu.getProType ()); menuOrgRepository.save (menuOrg); if (menu.getParentId ()! = null) {parentIdSet1.add (menu.getParentId ()) }} / / determine whether parentIdSet1 is empty if (! CommonUtils.isCollectionBlankOrEmpty (parentIdSet1)) {packageParent (parentIdSet1);}} scenario two
Calculate the factorial of 5
Public class Test {public static void main (String [] args) {System.out.println (f (5));} public static int f (int n) {if (1 = = n) return 1; else return n * f (nmur1);}}
In this question, it is analyzed according to three conditions of recursion:
(1) Boundary condition: factorial, when multiplied to the last number, that is, 1, returns 1, and the program is executed to the end.
(2) Recursive forward segment: continue to call yourself when the current parameter is not equal to 1
(3) Recursive return segment: multiply from the largest number. If the current parameter is 5, then it is 54, that is, 5 (5-1), that is, n * (nMel 1).
Summary
There must be iterations in recursion, but there may not be recursion in iterations, and most of them can be converted into each other.
Can use iterative no recursion, recursive call function, the calculation is repeated, waste of space, and recursion is too deep easy to cause stack overflow.
Java Recursive algorithm I. Overview
Java recursion: simply put, the function itself directly or indirectly calls the function itself.
Second, application scenarios
If: a function is used repeatedly, and each time it is used, the result of participating in the operation is related to the last call, then recursion can be used to solve this problem.
Key points of use:
1. The condition of recursion must be clear. Otherwise, it is easy to stack overflow.
2. Notice the number of recursions.
III. Examples
The simplest recursive demonstration
Public class recursionDemo {public static void main (String [] args) {show ();} private static void show () {method ();} private static void method () {show ();}} IV. Practical example
We all know that the binary of 6 is 110, so how does the program execute?
Code example:
Public static void main (String [] args) {toBin (6);} private static void toBin (int num) {if (num > 0) {/ / System.out.println (num%2); toBin (num/2);}}
Run the process:
Recursive demonstration 2: calculating 1-5, summing
Public static void main (String [] args) {/ / 1-5 summation int sum = getSum (5); System.out.println (sum);} private static int getSum (int num) {int xanth9; if (num==1) {return 1;} return num+getSum (num-1);}
Program operation diagram:
Fifth, the shortcomings of recursion
When using recursion, be sure to consider the number of recursions, responsible for it can easily cause virtual machine "stack overflow".
Still use the summation code above, but this time change the summation cardinality to 90000000 and see how it turns out.
Public static void main (String [] args) {/ / 1-90000000 summation int sum = getSum (90000000); System.out.println (sum);} private static int getSum (int num) {int xanth9; if (num==1) {return 1;} return num+getSum (num-1);}
Sure enough, it caused the virtual machine stack overflow.
The above is all the content of the article "how to use Recursion in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.