Java语法-递归

本文探讨了递归的概念,包括直接递归和间接递归,并强调了使用递归时必须明确的两个前提:结束条件和目的。通过1到n的累加和问题解析递归目标,并给出实现递归打印多层目录的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、什么是直接递归?什么是间接递归?

直接递归是A方法调用A方法;
间接递归是A方法调用B方法,B方法又回头调用A方法;

2、使用递归必须明确的前提是什么?

(1)递归的结束条件;
(2)递归的目的;

3、在1~n 累加和的题目中,递归的目的是获取下一个被加的数字(n-1),如何理解?

public static int sum(int n){
        if(n==1)return 1;
        else {
            return n + sum(n-1);
        }
    }

这里递归的目的等同于已经在A方法中被调用的A方法的作用 —— sum(n-1);
并不是 return 后面的 n+sum(n-1) 或者 sum(n);

4、实现递归打印多层目录

public class test {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\SDU214\\Desktop\\论文");
        getAllFiles(file);

    }

    public static void getAllFiles(File file){
        System.out.println(file);
        File[] arr = file.listFiles();
        for (File s : arr) {
            if(s.isDirectory()){
                getAllFiles(s);
            }
            else {
                System.out.println(s);
            }
        }
    }
}

5、题目如下
在这里插入图片描述

public class test {
    public static void main(String[] args) {
        File file = new File("D:\\某某某");
        getAllFiles(file);

    }

    public static void getAllFiles(File file){

        File[] arr = file.listFiles();
        for (File f : arr) {
            if(f.isDirectory()){
                getAllFiles(f);
            }
            else {
                String str = f.getName();
                if(str.endsWith(".pdf")){
                    System.out.println(str);
                }
            }
        }
    }
}
1.A theory of learning from different domains.pdf
The Extended Cohn-Kanade Dataset (CK+).pdf
CNN_book_small.pdf
xgboost导读和实战.pdf
IEEEtran_bst_HOWTO.pdf
tux.pdf
IEEEtran_HOWTO.pdf
jrnl.pdf
testflow_ctl_A4.pdf
testflow_ctl_LTR.pdf
testflow_doc.pdf
ex1.pdf
ex2.pdf
ex3.pdf
ex4.pdf
ex5.pdf
ex6.pdf
template1.pdf
template2.pdf
template3.pdf
test.pdf
test1.pdf
test2.pdf
test2_1.pdf
test3.pdf
test4.pdf
test6.pdf
ge.pdf
[muchong.com]A-Practical-Guide-to-LATEX-Tips.pdf
[muchong.com]LATEX_排版学习笔记.pdf
刘海洋《LaTeX入门》.pdf
IEEEtran_HOWTO.pdf
jrnl.pdf
刘海洋《LaTeX入门》.pdf
17Lbp with six intersection points Reducing redundant information in lbp-top for micro-expression recognition.pdf
7micro-expression recognition based on CBP-TOP feature with ELM.pdf
LOCP-TOP.pdf
VLBP.pdf
ssvm.pdf
SSVR_TKDE.pdf
UD4SVM013006.pdf
ARX1.pdf
Internet数据挖掘现状及未来发展_陈永政.pdf
中国地源热泵技术现状及发展趋势_徐伟.pdf
关于地源热泵应用问题探讨_尹丽媛.pdf
关于地源热泵技术发展现状的研究_赵风丽.pdf
回归分析在地源热泵系统地温场研究中的应用_刘红卫.pdf
国内地源热泵应用情况调查报告_吕悦.pdf
国外数据挖掘应用研究与发展分析_滕广青.pdf
地源热泵的特点及发展前景_龚伟申.pdf
地源热泵的研究与应用现状_武曈.pdf
我国地源热泵的研究现状及展望_杨爱.pdf
数据挖掘概述_徐承俊.pdf
数据挖掘综述_汪明.pdf
数据挖掘综述_王梦雪.pdf
机器学习算法PPT.pdf
环保节能地源热泵技术应用研究_仉安娜.pdf
银川地区地埋管地源热泵开发利用前景分析_杨超.pdf
毕业论文 正式3(黄天欢).pdf
Seasonal Analysis and Prediction of Wind Energy Using Random Forests and ARX Model Structures.pdf
Seasonal Analysis and Prediction of Wind Energy Using.pdf
xgboost导读和实战.pdf
一种地源热泵系统性能预测方法.pdf
毕业论文 正式3(黄天欢).pdf
天欢,吉林大学学报0050_O_editing.pdf
TutorialForFarutoUltimate3.1.pdf
Java数据结构和算法.(第二版).pdf
2.1.1【逻辑填空】专项突破解题技巧.pdf
2.1.2【语言表达】专项突破解题技巧.pdf
2.1.3【片段阅读】专项突破解题技巧.pdf
2.1.4【文章阅读】专项突破解题技巧.pdf
2.2.1【数学运算】专项突破解题技巧.pdf
2.2.2【数字推算】专项突破解题技巧.pdf
2.3.1【图形推理】专项突破解题技巧.pdf
2.3.2【定义判断】专项突破解题技巧.pdf
2.3.3【逻辑推理】专项突破解题技巧.pdf
2.3.4【类比推理】专项突破解题技巧.pdf
2.4 【资料分析】专项突破解题技巧.pdf
招商银行公司综合介绍汇总.pdf
证明材料.pdf
class_c_cross_parameter.pdf
class_c_cross_p_l_k_parameter.pdf
class_c_cross_robust_flow_parameter.pdf
class_c_local_flow_parameter.pdf
class_c_p_l_k_parameter.pdf
class_c_robust_flow_parameter.pdf
class_feature_tracker_context.pdf
07813187.pdf
2018-5-12-07374704.pdf
2018-5-12-07379458.pdf
2018-5-12-25958-1.pdf
2018-5-12p237-song.pdf
2018.2.7.pdf
2018.3.5.1.pdf
2018.3.5.2.pdf
2018.3.5.3.pdf
6412073f9016e36212052f6192bd2bc8cc2c.pdf
Baltrusaitis_Constrained_Local_Neural_2013_ICCV_paper.pdf
CVWW2017_paper_17.pdf
FG2015_camera_ready.pdf
thebook.pdf
Wood_Rendering_of_Eyes_ICCV_2015_paper.pdf
1.pdf
10.pdf
11.pdf
12.pdf
13.pdf
14.pdf
15.pdf
16.pdf
17.pdf
18.pdf
19.pdf
2.pdf
20.pdf
21.pdf
22.pdf
23.pdf
24.pdf
25.pdf
26.pdf
27.pdf
28.pdf
29.pdf
3.pdf
30.pdf
33.pdf
34.pdf
35.pdf
36.pdf
37.pdf
4.pdf
5.pdf
6.pdf
7.pdf
8.pdf
9.pdf
1A Main Directional Mean Optical Flow Feature.pdf
2Microexpression Identification and Categorization using a Facial Dynamics Map.pdf
3DSIFT.pdf
3Micro-Expression Recognition Using Color Spaces.pdf
1.pdf
11.pdf
12.pdf
13.pdf
14.pdf
15.pdf
16.pdf
17.pdf
2.pdf
22.pdf
23.pdf
24.pdf
25.pdf
3.pdf
5.pdf
8.pdf
9.pdf
05403044.pdf
05771451.pdf
06977014.pdf
1-s2.0-S1077314215002702-main.pdf
1.pdf
11.pdf
24.pdf
24Reading Hidden Emotions Spontaneous.pdf
9.pdf
chp%3A10.1007%2F978-3-319-16178-5_20.pdf
chp%3A10.1007%2F978-3-319-25903-1_32.pdf
s1-ln4119682133020548-1939656818Hwf709713063IdV-973545650411968PDF_HI0001.pdf
Discriminative SpatiotemporalLocal Binary (1).pdf
NEUCOM-D-17-01090 (1).pdf
sparsity_tac16.pdf
凸优化教材.pdf
支持向量机通俗导论(理解SVM的三层境界)Latex版.pdf
07064006.pdf
07521414.pdf
english.pdf
互联网发展对中国经济增长的影响研究.pdf
互联网教育发展前景分析.pdf
研究生英语写作-黄天欢(正式).pdf
MATLAB神经网络30个案例分析.pdf
MATLAB神经网络43个案例分析.pdf
MATLAB神经网络原理与实例精解.pdf
01第一章.pdf
SAMPLE.pdf
01第一章.pdf
SAMPLE.pdf
F辑中文投稿指南(2016第1期)参考文献.pdf
sciscn.pdf
期刊论文模板-双人.pdf
1-TPAMI-0690-1205-2.pdf
2 Interactive Image Segmentation2012.pdf
3.GuidedFilterPAMI2013.pdf
4. JCST_2017-1-5-6580.pdf
5.CON.Robust Optical Flow for Driver Assistance.pdf
6.中国科学N112016-00198.pdf
7.2016-Traffic-sign-detection-in-MLS-acquired-point-clouds-for-geometric-and-image-based-semantic-inventory_2016_ISPRS-Journal-of-Photogrammetry-and.pdf
8. Wp H 3-D hologram displays in smartphone and TV.pdf
9 automatic-landmark-identification_January06.pdf
F辑中文投稿指南(2016第1期)参考文献.pdf
期刊论文模板-双人.pdf
英语科技论文与会议简报.pdf
Duda-problemsolutions.pdf
ch2.pdf
ch3.pdf
ch38.pdf
ch4.pdf
第1题更详细的证明.pdf
第三章 最大似然估计和贝叶斯参数估计习题答案.pdf
第二章 贝叶斯决策论习题答案.pdf
第五章 线性判别函数习题答案.pdf
第四章 非参数技术习题答案.pdf
eetop.cn_John.Wiley.and.Sons.Essentials.of.Error.Control.Coding.Sep.2006.pdf
Matrix Analysis for Scientists and Engineers.pdf
Chapter1 Introduction (print version).pdf
Chapter1 Introduction.pdf
Chapter10 Unsupervised Learning and Clustering (print version).pdf
Chapter10 Unsupervised Learning and Clustering.pdf
Chapter2 Bayesian Decision Theory.pdf
Chapter2 Bayesian_Decision Theory(print version).pdf
Chapter3 Maximum-Likelihood and Bayesian Parameter Estimation.pdf
Chapter3 Maximum-Likelihood and Bayesian Parameter Estimation(print version).pdf
Chapter4 Nonparametric Techniques.pdf
Chapter4 Nonparametric Techniques(print version).pdf
Chapter5 Linear Discriminant Functions.pdf
Chapter5 Linear Discriminant Functions(print version).pdf
SolutionsManual-Statistical+and+Adaptive+Signal+-习题答案Processing.pdf
Statistical and Adaptive Signal Processing_notes .pdf
Statistical+and+Adaptive+Signal+Processing_notes+2016.pdf
专业外语1.pdf
概率-Probability+and+Random+Processes+3ed-G.+Grimmett,+D.+Stirzaker+(Oxford,+2001)+清晰版.pdf
模式分类.pdf
哈工大模式识别课件—第5章线性判别函数.pdf
模式识别--第三讲-贝叶斯分类器.pdf
模式识别-线性判别函数.pdf
非参数技术.pdf
简二毛-统计与自适应信号处理.pdf
统计与自适应信号处理.pdf
模式识别练习题.pdf
2011随机过程-chapter1马丕明老师.pdf
2011随机过程-chapter2马老师.pdf
2011随机过程-chapter3马老师.pdf
2011随机过程-chapter4马老师.pdf
2011随机过程-chapter5.pdf
随机过程历次作业答案整理.pdf
1707.05956.pdf
A Survey on Transfer Learning.pdf
CNN_book_small-1.pdf
07172522.pdf
8256-38279-1-PB.pdf
The Augmented Lagrange Multiplier Method for.pdf
Latent Low-Rank Representation for Subspace Segmentation.pdf
Robust Recovery of Subspace Structures.pdf
06413801.pdf
06751479.pdf
06247930.pdf
06618897.pdf
06618939.pdf
1_基于结构风险上界的SVM参数选择.pdf
Foundations of Machine Learning.pdf
JXT.pdf
Xianye Ben TIP.pdf
荆晓远论文.pdf
A nonlinear regression technique for manifold valued data with applications to.pdf
COMPARISON OF DIURNAL TEMPERATURE CYCLE MODEL AND POLYNOMIAL.pdf
Melt index prediction by support vector regression .pdf
regression techniques for modelling conception in seasonally calving dairy cows.pdf
Urban Building Density Estimation From.pdf
Variational Autoencoded Regression_ High Dimensional Regression of Visual Data on Complex Manifold.pdf
统计学习方法(李航).pdf
(2016.10.8)Multi-label Active Learning Based on Maximum.pdf
迁移学习手册.pdf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值