Traversing directories without using recursion?

后端 未结 8 1949
无人及你
无人及你 2021-01-18 06:46

The Problem I need to write a simple software that, giving certain constraints, appends to a list a series of files. The user could choose between

8条回答
  •  有刺的猬
    2021-01-18 07:12

    My iterative solution:

      ArrayDeque stack = new ArrayDeque();
    
        stack.push(new File(""));
    
        int n = 0;
    
        while(!stack.isEmpty()){
    
            n++;
            File file = stack.pop();
    
            System.err.println(file);
    
            File[] files = file.listFiles();
    
            for(File f: files){
    
                if(f.isHidden()) continue;
    
                if(f.isDirectory()){
                    stack.push(f);
                    continue;
                }
    
                n++;
                System.out.println(f);
    
    
            }
    
        }
    
        System.out.println(n);
    

提交回复
热议问题