java中的递归

什么是方法的递归?

举例:方法不能结束,一直在栈内进行压栈,会出现错误。StackOverflowError(方法自身调用自生,递归的方法是很耗费占内存的,能不用就不用)递归要有结束的条件,没有结束条件的话,就会出现栈内存溢出错误。

public class Test {
    public static void main(String[] args) {
        System.out.println("main begin");
        function();
    }
    public static void function(){
        System.out.println("function begin");
        function();
        System.out.println("function over");
    }
}

递归的结束条件(弹栈)

注意:即使有了结束的条件,条件结束条件正确,但是还是发生了溢出的错误,原因是递归的太深。
举例:利用递归进行求和。

/**
 * 利用递归的方法来进行求和
 * 4 + 3 + 2 + 1
 *
 * @author kiosk
 */
public class Test3 {
    public static void main(String[] args) {
      int sum = sum(4);
        System.out.println(sum);
    }

    /**
     * 利用递归的方法
     *
     * @return
     */
    public static int sum(int n) {
        //结束条件 n==1的时候结束
        if (n == 1) {
            return 1;
        } else {
            //要将这个结果返回
            // n + sum(n-1)
            // 4 + sum(3)  return
            // 4 + 3 + sum(2) return
            // 4 + 3 + 2 + sum(1)m return
            // 4 + 3 + 2 + 1
            return n + sum(n - 1);
        }
    }
}

利用递归的方法来计算阶乘

package com.westos.demo2;

/**
 * 阶乘的计算 利用递归
 *
 * @author kiosk
 */
public class Test4 {
    public static void main(String[] args) {
        System.out.println("5的阶乘为:" + result(5));
    }

    public static int result(int x) {
        if (x == 1) {
            return 1;
        } else {
            return x * result(x - 1);
        }
    }
}

递归的应用:如何将一个目录以及目录下的文件复制给另一个目录下去

代码如下:
可以将遍历文件夹和IO复制的操作分开

import java.io.*;

/**
 * 使用的方法:递归的思想(如果是文件的话,那么就将文件进行复制,
 * 如果是目录的话,就继续寻找然后遍历复制)
 * 将一个文件夹以及文件内容 复制到另一个文件夹之下
 *
 * @author kiosk
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        Test3 test = new Test3();
        test.copy("D:\\test", "C:\\test");
    }

    /**
     * target:目标文件夹的位置
     * source:源文件夹的位置
     *
     * @param source
     * @param target
     */
    public void copy(String source, String target) throws IOException {
        File file1 = new File(source);
        if (!file1.exists()) {
            System.out.println("这个文件是不存在的");
        }
        File file2 = new File(target);
        if (!file2.exists()) {
            //如果文件夹是不存在的话,那么就将这个文件创建
            file2.mkdir();
        }
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] bytes = new byte[1024];
        int read;
        //列出的是目标文件夹下的文件
        File[] files1 = file1.listFiles();
        for (File file : files1) {
            //如果是文件目录的话继续进行copy方法(递归)
            if (file.isDirectory()) {
                System.out.println("文件目录的名字为:" + file.getName());
                copy(source + "\\" + file.getName(), target + "\\" + file.getName());
                System.out.println( file.getName());
            } else {
                //如果不是文件目录,就对文件进行复制
                fis = new FileInputStream(file);
                System.out.println("文件名为:" + target + file.getName());

                fos = new FileOutputStream(target + "\\" + file.getName());
                while ((read = fis.read(bytes)) != -1) {
                    fos.write(bytes);
                }
            }
        }
        //关闭流
        if (fis != null) {
            fis.close();
        }
        if (fos != null) {
            fos.close();
        }


    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40843624/article/details/86549223