Traversing directories without using recursion?

后端 未结 8 1961
无人及你
无人及你 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:16

    PARTY, thank you for advice. I transformed a little bit your code, that's what I've got

    private ArrayList displayDirectory(File node){
    ArrayList FileList = new ArrayList();
    ArrayList directories = new ArrayList();
    if (node.isDirectory())
       directories.add(node);
    // Iterate over the directory list
    Iterator it = directories.iterator();
    for (int i = 0 ; i < directories.size();i++){
       File dir  =  directories.get(i);
       // get childs
       String[] subNode = dir.list();
       for(int j = 0 ; j < subNode.length;j++){
          File F = new File( directories.get(i).getAbsolutePath(), subNode[j]);
          // display current child name
        //  System.out.println(F.getAbsoluteFile());
          // if directory : add current child to the list of dir to process
          if (F.isDirectory()) directories.add(F);           
            else   FileList.add(F);
          }
    }   
    return FileList;
    }
    

提交回复
热议问题