In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is an example analysis of the important and difficult points of Java recursion. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
What is recursion?
It is a method that calls itself when it is executed.
Recursive requirements:
1 there is a condition close to termination
2 to realize recursion, it is necessary to derive a recursive formula.
Recursion is to pass it down and come back up. Find the factorial of 5, code example:
Public static int fact (int n) {if (n = 1) {return n;} return n*fact (n-1);} public static void main (String [] args) {int n = 5; int ret = fact (n); System.out.println (ret);}
The termination condition here is that when n equals 1, the return of 1 stops recursion. Otherwise, recursion will cause overflow of the stack area.
Analyze the process of recursion
The recursion process of the above code is analyzed here.
As shown in the figure above, this is the recursive process of the recursive code above. Passing in 5 first does not meet the if condition. So continue recursion until it is recursive to n = = 1, which is no longer recursive, but returns 1. At this point, hand it down to the end. Start to return, when you return, finally return 1, and then return all the way until the end of the last return, that is, return 120.
Recursive exercises print each bit of a number sequentially
For example: 1234 prints out 1234 code examples:
Public static void print1 (int n) {if (n = 0) {return;} print1 (n / 10); System.out.print (n% 10 + ");} public static void main (String [] args) {int n = 1234; print1 (n);}
The whole recursive process is as follows:
The above is the example analysis of the important and difficult points of Java recursion. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.