zoukankan      html  css  js  c++  java
  • java递归的几种用法

    以前一直对递归发怵,一想到它就“浑身哆嗦”,但是递归确实是非常精髓和精妙的,有时候处理问题会非常的方便。比如排序,遍历目录下的文件什么的,但是老这么怕他也不是个办法...

    先看一个排序的:
    现有122345六个数,要求用一个main函数实现所有不同的排序并打印出来,要求:4不能在第三位,3和5不能相连(某公司笔试题)

        public static List list = new ArrayList();
        /**
         * 构造字符串的所有排序组合
         *
         * @param str
         *            将要组合成的字符
         * @param nstr
         *            源字符串集
         */
        public static void group(String str, String nstr) {

            if (str.length() != nstr.length()) {// 如果要生产的字符串不等于原字符串的长度
                String rest = getRest(str, nstr);// 调用转换函数,该方法是得到除了当前字符串以后剩下的字符
                for (int i = 0; i < rest.length(); i++) {
                    String temp = str + rest.substring(i, i + 1);

                    if (temp.indexOf("4") != 2 && temp.indexOf("35") == -1
                            && temp.indexOf("53") == -1) {
                        // 过滤显示条件,如果去掉此处的判断,就是列出所有字符集的排列组合

                        if (!list.contains(temp) && temp.length() == nstr.length()) {
                            System.out.println(temp);
                            list.add(temp);
                        }
                        group(temp, nstr);
                    }

                }
            }
        }
        /**
         * 从源字符串集中去除将要组合成的字符
         *
         * @param str
         *            将要组合成的字符
         * @param nstr
         *            源字符串集
         * @return 剩余字符串集
         */
        public static String getRest(String str, String nstr) {

            String rest = "";
            if (str.length() < nstr.length()) {// 如果将要组合的字符串<元字符串的长度
                rest = nstr;// 将原字符串赋给rest
                for (int i = 0; i < str.length(); i++) {
                    rest = rest.replaceFirst(str.substring(i, i + 1), "");// 把字符串的第I个替换为""
                    // 注意此处的replaceFirst,而不是replaceAll
                }
            }
            return rest;
        }
    再就是关于目录结构遍历的了:

    static void getDir(String str){

            File f = new File(str);
           

            if(f.isDirectory()){
                File[] files = f.listFiles();
                for(int i=0;i<files.length;i++){
                    if(files[i].isDirectory()){
                        getDir(files[i].getPath());
                        System.out.println("--"+files[i].getPath());
                    }
                }
            for(int j=0;j<files.length;j++){
                if(files[j].isFile()){
                    System.out.println("   "+files[j].getName());
                }
            }
        }java-javascript 风之境地

  • 相关阅读:
    未能加载文件或程序集“System.EnterpriseServices, Version=4.0.0.0或2.0.0.0
    解决本地调用office组件成功,但是发布到IIS中出现的错误(检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败)
    未能找到类型或命名空间名称“Coco”(是否缺少 using 指令或程序集引用)
    SQL截取字段字符串的方法
    C# 128位AES 加密解密 (转)
    检索 COM 类工厂中 CLSID 为 {13C28AD0-F195-4319-B7D7-A1BDAA329FB8} 的组件时失败,原因是出现以下错误: 80040154
    js 获取时间比较全,留备用(zhuan)
    windows之如何把iso文件转换为VHD文件
    python之三行代码发送邮件
    RobotFramework第二篇之web自动化
  • 原文地址:https://www.cnblogs.com/sky7034/p/2211235.html
Copyright © 2011-2022 走看看