Java基础学习:5、递归

1、递归:就是方法自己调用自己。

public class Test01 {
  public void test(int n) {
    if (n > 2) {
      test(n -1);
    }
    System.out.println(n);
  }

  public static void main(String[] args) {
    Test01 tes = new Test01();
    tes.test(4);
  }
}

2、递归规则:

 

递归过程和栈的工作原理一致,递归调用就是通过栈这种数据结构完成的,整个过程实际上就是一个栈的入栈和出栈问题。

那么递归中的“递”就是入栈,递进;“归”就是出栈,回归。

3、递归示意图:

  每调用一次方法开辟一个新的独立空间。

 

posted @ 2021-07-22 15:52  Y字仇杀队  阅读(64)  评论(0编辑  收藏  举报