In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how Java uses recursive algorithms. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. The basic idea of java recursive algorithm. Java recursive algorithm is a recursive algorithm based on Java language. A recursive algorithm is an algorithm that calls its own function or method directly or indirectly. The essence of the recursive algorithm is to decompose the problem into sub-problems of the same kind of reduced scale, and then recursively call the method to represent the solution of the problem. Recursion can often bring us a very simple and intuitive form of code, which greatly simplifies our code. however, recursive thinking is really contrary to our conventional thinking, which is usually a top-down thinking problem. the recursive trend is to think from the bottom up.
2. The characteristics of java recursive algorithm for solving problems.
Recursion is to call itself in a method.
When using a recursive policy, there must be an explicit recursive end condition, called a recursive exit.
The code of the recursive algorithm is very simple, but the running efficiency of the recursive algorithm is low. So recursive programming is not recommended.
In the process of recursive call, the system opens up a stack to store the return points and local quantities of each layer. Too many times of recursion can easily cause stack overflow, so recursive algorithms are generally not recommended to design programs.
When doing a recursive algorithm, we must grasp the exit, that is, to do a recursive algorithm must have a clear recursive end condition. This is very important. In fact, this exit is a condition, when this condition is met, we will no longer recursive.
3. Java recursive algorithm code example:
Public class Factorial {
/ / this is a recursive function
Int fact (int n) {
If (naughty 1) return 1
Return fact (nmur1) * n
}
}
Public class TestFactorial {
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
Factorial factorial=new Factorial ()
System.out.println ("factorial (5) =" + factorial.fact (5))
}
}
Thank you for reading! This is the end of this article on "how to use recursive algorithms in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.