面试题目两题写法

第一题:

题目打印:
面试题目两题写法

java写法:

  public static void showTree(int level, File parentFolderPath) {
        if (parentFolderPath.isDirectory()) {
            File[] childFiles = parentFolderPath.listFiles();
            for (File file : childFiles) {
                showNameByLevel(level);
                System.out.println(file.getName());
                if (file.isDirectory()) {
                    showTree(level + 1, file);
                }
            }
        }
    }
    public static void showNameByLevel(int level) {
        StringBuffer stringBuffer = new StringBuffer();
        if (level > 0) {
            for (int i = 0; i < level; i++) {
                stringBuffer.append("\t");
            }
        }
        if(stringBuffer.length() >0) System.out.print("|" + stringBuffer);
        System.out.println("|");
        if (stringBuffer.length()>0) System.out.print("|" + stringBuffer);
        System.out.print("----");
    }
    @Test
    public void c23() {
        File file = new File("D:\\TOOL\\IDEASpace\\mavedome\\src\\test\\java");
//        printlen(file, 0);
        showTree(0, file);
    }

python写法:

第二提:

猜你喜欢

转载自blog.51cto.com/357712148/2342053