Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of Java recursion and how to use it

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the concept of Java recursion and how to use it". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the concept of Java recursion and how to use it" can help you solve the problem.

First, the concept of recursion 1. What is recursion?

Recursion is the process by which a method invokes a method itself.

There are two prerequisites for using recursion:

1. There is a condition for approach and termination.

two。 Call yourself.

How to implement recursion?

The most important way is to achieve recursion, which needs to derive a recursive formula.

The way of thinking recursively: think horizontally, think according to the recursive formula.

Code execution: vertical execution.

two。 Recursive explanation

First, take a look at the following code:

Public class TestDemo {public static void func () {func (); / / call yourself} public static void main (String [] args) {func ();}}

The code above is a simple recursion.

Let's take a look at the running result of this code again.

Draw and explain:

For the recursion in the figure above, there is no condition that tends to terminate at all, so the function will be recursive endlessly. Each recursion will open up memory on the stack, always open up memory on the stack, and there will always be a stack out.

Remember, brothers: once there is something wrong with your recursion, you will report one if the boundary is not found correctly.

If you report this error, then there must be an error in your termination condition, or the failure to write the termination condition caused you to go too deep in the process of recursion, resulting in a stack overflow.

If we want the above code to be correct, we need to add a termination condition to it.

The correct code is as follows:

Public class TestDemo {public static void func (int n) {if (n = = 1) return; func (n-1);} public static void main (String [] args) {func (3);}}

The following will give you a more in-depth understanding of recursion through simple examples

Second, the use of recursion

Example: factorial drawing analysis of finding n by recursion:

Implementation code:

Public class TestDemo {public static int fac (int n) {if (n = = 1) {return 1;} int tmp = n * fac (n-1); return tmp;} public static void main (String [] args) {System.out.println (fac (5));}}

Code drawing explanation:

Example: finding the sum of n

Drawing analysis:

Implementation code:

The first is public class TestDemo {public static int sumAdd (int n) {if (n = = 1) {return 1;} int tmp = n + sumAdd (n-1); return tmp;} public static void main (String [] args) {System.out.println (sumAdd (3)) The second way of writing: public class TestDemo {public static int sumAdd (int n) {if (n = = 1) {return 1;} return n + sumAdd (n-1);} public static void main (String [] args) {System.out.println (sumAdd (3));}}

Example: recursive implementation prints each digit sequentially

Drawing analysis:

Implementation code:

Public class TestDemo {public static void print (int n) {if (n < 10) {System.out.print (n + ");} else {print (n +"); System.out.print (n + ");}} public static void main (String [] args) {print (1234);}}

Example: write a recursive method, enter a non-negative integer, and return the sum of the numbers that make up it. For example, if you enter 1729, you should return 1 "7" 2 "9"

Implementation code:

Public class TestDemo {public static int sumEveryone (int n) {if (n < 10) {return n;} else {return n + sumEveryone (nmax 10);}} public static void main (String [] args) {System.out.println (sumEveryone (7910));}}

Example: what is the nth Fibonacci number

Drawing analysis:

Implementation code:

The first method: recursive public class TestDemo {public static int fib (int n) {if (n = = 1 | | n = = 2) {return 1;} else {return fib (nMuth2) + fib (nMuth1);}} public static void main (String [] args) {System.out.println (fib (5)) } the second method is called loop (iterative) implementation of public static int fib2 (int n) {if (n = 1 | | n = 1) {return 1;} int f1 = 1; int f2 = 1; int f3 = 0; for (int I = 3; I < n; iF3 +) {f3 = f1+f2 F1 = f2; f2 = f3;} return f3;} public static void main (String [] args) {System.out.println (fib2 (45));} that's all about "what is the concept of Java recursion and how to use it". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report