Recursive thinking --- delete folders

  • Recursive thinking
package com.uncle.test_io.newtestfile;

public class TestMethod {
    
    

//    public void testOne(){
    
    
//        this.testTwo();
//        System.out.println("我是testOne方法");
//    }
//    public void testTwo(){
    
    
//        this.testThree();
//        System.out.println("我是testTwo方法");
//    }
//    public void testThree(){
    
    
//        System.out.println("我是testThree方法");
//    }

    //递归---->本质是方法的调用
    //      设计一个方法  描述盖宝塔
//    public void buildTower(int floor){//5
//        for(int i=1;i<=floor;i++){
    
    
//            System.out.println("盖到第"+floor+"层宝塔啦");
//        }
//    }

    //递归的想法
    //  想让我做事(盖第五层) 我要求让别人把之前的事情做完
    //      1.让别人先做事
    //      2.我自己做事
    public void buildTower(int floor){
    
    //5
        //判断当前floor是否为1 若不是找一个别人先盖之前层
        if(floor>1){
    
    
            this.buildTower(floor-1);
        }
        //我自己做事
        System.out.println("盖到第"+floor+"层宝塔啦");
    }
//    public void buildTower(int floor){//4
//        //找一个别人先盖3层
//        this.buildTower(floor-1);
//        //我自己做事
//        System.out.println("盖到第"+floor+"层宝塔啦");
//    }
//    public void buildTower(int floor){//3
//        //找一个别人先盖2层
//        this.buildTower(floor-1);
//        //我自己做事
//        System.out.println("盖到第"+floor+"层宝塔啦");
//    }
//    public void buildTower(int floor){//2
//        //找一个别人先盖1层
//        this.buildTower(floor-1);
//        //我自己做事
//        System.out.println("盖到第"+floor+"层宝塔啦");
//    }
//    public void buildTower(int floor){//1
//        //我自己做事
//        System.out.println("盖到第"+floor+"层宝塔啦");
//    }


    public static void main(String[] args){
    
    
        //加载TestMethod类的过程
        TestMethod tm = new TestMethod();
        tm.buildTower(5);//执行一次
//        tm.testOne();//调用testOne让其执行一遍--->栈内存中 临时
    }

}

Recursive thinking of deleting folders (don’t play around, File’s delete method really deletes them completely)

package com.uncle.test_io.newtestfile;

import java.io.File;

public class NewTestFile {
    
    

    //设计一个方法  用来展示(遍历)文件夹
    //  参数-->file(代表文件或文件夹)
    public void showFile(File file){
    
    //D://test文件夹
        //判断如果file是一个文件夹 文件夹内有元素 找一个人先做
        //获取file的子元素  files==null是个文件 files!=null是个文件夹 files.length!=0是一个带元素的文件夹
        File[] files = file.listFiles();//test文件夹所有子元素
        if(files!=null && files.length!=0){
    
    
            for(File f:files){
    
    //每一个子元素都找人遍历
                this.showFile(f);//循环第一次 test文件夹中的aaa 循环第二次 test->bbb 循环第三次 Test.txt
            }
        }
        //做自己的显示(file是文件或file是一个空的文件夹)
        System.out.println(file.getAbsolutePath());
    }

    //设计一个方法 删除文件夹
    //  参数 file
    public void deleteFile(File file){
    
    
        //判断file不是空文件夹 找人先做事
        File[] files = file.listFiles();
        if(files!=null && files.length!=0){
    
    
            for(File f:files){
    
    
                this.deleteFile(f);
            }
        }
        //删除file (file是个文件或file是一个空文件夹)
        file.delete();
    }

    public static void main(String[] args){
    
    
        NewTestFile ntf = new NewTestFile();
        File file = new File("D://test");
        ntf.deleteFile(file);
        //ntf.showFile(file);
    }
}

  • Recursive core idea
    Insert picture description here
  • The difference between circular thinking and recursive thinking
    Insert picture description here
  • Normal method call process
    Insert picture description here
  • The execution sequence of the delete folder method (see the path in the code)
    Insert picture description here

Guess you like

Origin blog.csdn.net/m0_51945027/article/details/112970654