Recursive interpretation in Java (Recursive implementation of the 99 multiplication table to explain) _java

Source: Internet
Author: User

1: Ordinary implementation of the 99 multiplication table is too simple, is a programmer will be, implemented as follows:

Package test.ms;

public class Test99 {public
 
  static void Main (string[] args) {for
 
  (int i=1; i<=9;i++) {for
   (int j=1; j<=i ; J + +) {
   System.out.print (j+ "*" +i+ "=" + (I*J) + "");
   }
   System.out.println ();}}
  


2: Implement the 99 multiplication table recursively
the code is as follows:

Package test.ms;

public class Multitable {public
 static void Main (String args[]) { 
    m (9); 
  } 
 
  /** 
   * Print out 99 multiplication table 
   * @param i * * public 
  static void m (int i) { 
    if (i = = 1) { 
      System.out.println ( "1*1=1"); 
    } else { 
      m (i-1); 
      for (int j = 1; J <= I; j +) { 
        System.out.print (j + "*" + i + "=" + J * i + ""); 
      } 
      System.out.println ();}}  


Recursive way to invoke the diagram:

Each method call will generate a stack frame, pushed into the method stack, when recursive call, the stack frame in the method stack diagram and the diagram above is similar.
It is more intuitive to remove the reference relationship from the stack frame in the method, as shown in the following illustration:

simplifies the corresponding method call final execution as shown in the above illustration, note that I always change j every time starting from 1 and then incrementing to and I equal.
In this way, the above diagram is followed by a stack of 99 multiplication tables:

Summarize:

Nested for loops and comparisons with recursive implementations:

Stack is mainly used to store stack frames, each implementation of a method will appear pressure stack operation, therefore, the use of recursion to generate more stack frames, recursion will affect the memory, very memory consumption, and use for the loop to execute a method, press into the stack frame once, there is only one stack frame, so compare save memory.

Welcome to mercilessly Pat Bricks. Until you hit the halo.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.