如何遍历并将对象添加到我在之前的递归方法运行中创建的对象列表中?

How can I traverse back and add an object into an object list that I created in a previous runs of my recursive method?

All this program is trying to achieve is listing files as well as folders in a correct fashion , so that folders are assigned their own lists of objects.Files are excluded from having a list of objects as they won't contain any files or folders inside of themselves. My main concern here is that the objects that are being recursively read and written in the data.class, that the objects from the main , or rather "root", are not being all joined into one , consecutive list of objects at the end. Please help.

package bstTest2;
import java.io.*;
public class Main {
    public static void main(String[] args) {
        String filename = "G:/a";
        File f = new File(filename);        

        data d= new data();
        d.explore(f);
    }
}

package bstTest2;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class Node{ 
    private String filename;
    private long size;
    private List<Node> prevList;
    private List<Node> curList;
    private List<Node> chList;//child list of the current node - could be null if the node is a file 
    private List<Node> root;

    Node(String fn, long s,List<Node> previousList, List<Node> currentList,List<Node> childList){
        filename = fn;
        size = s;
        setPrevList(previousList);
        setCurList(currentList);
        setChList(childList);
    } 

    public Node() {
        initList();
    }

    public void initList(){
        curList = new LinkedList<Node>();
        setRoot(curList);
    }

    public void createNode(File file){
        curList.add(new Node(file.getName(),file.length(),null,curList,null));
    }


    public void createNodeList(File file){
        curList.add(new Node(file.getName(),file.length(),
            prevList,
        setCurList(chList), setChList(setCurList(new LinkedList<Node>()))));
    } 


    public List<Node> getChList() {
        return chList;
    }

    public List<Node> setChList(List<Node> chList) {
        this.chList = chList;
        return chList;
    }

    public List<Node> getCurList() {
        return curList;
    }

    public List<Node> setCurList(List<Node> curList) {
        this.curList = curList;
        return curList;
    }

    public List<Node> getRoot() {
        return root;
    }

    public void setRoot(List<Node> root) {
        this.root = root;
    }

    public List<Node> getPrevList() {
        return prevList;
    }

    public void setPrevList(List<Node> prevList) {
        this.prevList = prevList;
    }
}

package bstTest2;
import java.io.File;
import java.util.*;
public class data {

    private Node node = new Node();
    private File f[];  

    public void explore(File dir){
        f=dir.listFiles();
        if(f!=null){
            for(File file:f){
                if(!file.isDirectory()){    


                        //System.out.println("BFIL"+node.getCurList());
                        System.out.println("FILE: "+file.getName()+" "+file.length() +":");

                        node.createNode(file);

                        System.out.println(node.getCurList().size()+" AFIL"+node.getCurList());
                }else{


                        //System.out.println("BFOL"+node.getCurList());

                        System.out.println("FOLDER: "+file.getName()+" "+file.length() +":");

                        node.createNodeList(file);

                        System.out.println(node.getCurList().size() +" AFOL"+node.getCurList() );

                        explore(file);
                }

            }

        }

    }
}

您可以return 目录中包含的文件列表。这样做将允许您将该文件列表添加到目录节点。

为此,您可以将 expore(File dir) 方法更改为 return 目录中的节点列表,然后更新 explore(file); 调用以将该列表添加到目录节点.

public List<Node> explore(File directory) {
    List<Node> result = new ArrayList<Node>();

    for (File current : directory.listFiles()) {
        Node node = toNode(current);
        // do the real work here

        if (file.isDirectory()) {
            node.setChildren(explore(file));
        }

        result.add(node);
    }

    return result;
}

另一种方法是将目录节点传递给 explore(File dir) 方法并在该方法中更新它。然后,您可以将每个包含的文件或目录节点添加到该父节点。

public void explore(File directory, Node parent) {
    for (File current : directory.listFiles()) {
        Node node = toNode(current);
        // do the real work here

        if (file.isDirectory()) {
            explore(file, node);
        }

        parent.add(node);
    }
}