gpt4 book ai didi

java - 如何回溯并将对象添加到我在之前运行递归方法时创建的对象列表中?

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:28 24 4
gpt4 key购买 nike

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);
}

}

}

}
}

最佳答案

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

为此,您需要更改 expore(File dir) 方法以返回目录中的节点列表,并更新您的 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);
}
}

关于java - 如何回溯并将对象添加到我在之前运行递归方法时创建的对象列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com