递归

递归:指在当前方法内调用自己

递归分为两种,直接递归和间接递归

public static void main(String[] args) {
  System.out.println(get(15));
}
//用递归计算1-100的和
//100+(100-1)+(99-1)+...1
public static Integer get(int num){
  if(num==1){
    return 1;
  }
  return num*get(num-1);
}

 

public class MyFilter implements FileFilter{
  public boolean accept(File pathname) {
    //如果是文件夹,那么给放到File[]中
    if(pathname.isDirectory()){
      return true;
    }
    return pathname.getName().endsWith(".txt");
  }
}

public static void main(String[] args) {
  look(new File("D:\\java"));
}

public static void look(File file) {
  // 获取该路径下所有的文件和文件夹
  File[] files = file.listFiles(new MyFilter());
  // 遍历
  for (File f : files) {
    if (f.isDirectory()) {
      look(f);
    } else {

      System.out.println(f);
    }
  }
}

 

posted on 2019-06-17 09:42  boss-H  阅读(126)  评论(0编辑  收藏  举报

导航