我正在查询树,查找带有数据字符串“directory”的节点,然后在列表中报告其子项的数据。
在我的方法开始时,我已经将我的列表初始化了。 列表listofdirectories = new ArrayList<>();
在调试时,java会递归遍历树并将字符串添加到列表中,但是一旦它再次调用自身,listofdirectories变量在递归期间将再次变为空。但是,最后,它会列出根目录及其目录子目录。
我的方法没有参数。在pastebin上发布了代码,有很多System.out供我调试。
我的方法代码+控制台+示例树发布在这里: https://pastebin.com/9nXvcCNS
public List<String> getDirectories() // WORKS b/c didnt add first directory
{
List<String> listofdirectories = new ArrayList<>();
System.out.println("AT THE START OF THE METHOD " +listofdirectories);
// int sizeofchildrennodes = this.children.size();
if(this.data.equals("directory"))
{
System.out.println("FFFFFFFFFFFFFFF");
}
if(this.data.equals(("directory")))
{
listofdirectories.add(this.children.get(0).children.get(0).data);
for (int i = 0; i < this.children.size(); i++) { // Checks the directories in the CURRENT directory
String nodename = this.children.get(i).data;
FileSystemNode thenode = this.children.get(i);
System.out.println("Current node is: " + nodename);
System.out.println("Current nodedata is: " + thenode.children.get(0).data);
if(nodename.equals("directory2.0"))
{
System.out.println("I found direct2.0");
System.out.println("name of direct2.0 is " + thenode.children.get(0).children.get(0).data);
System.out.println("0" + listofdirectories);
listofdirectories.add(thenode.children.get(0).children.get(0).data);
System.out.println("0).5" +listofdirectories);
}
System.out.println("1" +listofdirectories);
if(nodename.equals("directory"))
{
System.out.println("@@" + listofdirectories);
listofdirectories.add(thenode.children.get(0).children.get(0).data);
if(thenode.children.size()> 0)
{
thenode.getDirectories();
}
}
}
// if(this.children.get(i).children.contains(i))
}
System.out.println("!!!beforeoutput" +listofdirectories);
return listofdirectories;
}
//////////////////////////////////
/// This is console's log
////////////////////////////////////
AT THE START OF THE METHOD []
FFFFFFFFFFFFFFF
Current node is: name
Current nodedata is: Movies and Pictures
1[Movies and Pictures]
Current node is: directory
Current nodedata is: name
1[Movies and Pictures]
@@[Movies and Pictures]
AT THE START OF THE METHOD []
FFFFFFFFFFFFFFF
Current node is: name
Current nodedata is: Movies
1[Movies]
Current node is: file
Current nodedata is: name
1[Movies]
Current node is: file
Current nodedata is: name
1[Movies]
Current node is: file
Current nodedata is: name
1[Movies]
!!!beforeoutput[Movies]
Current node is: directory
Current nodedata is: name
1[Movies and Pictures, Movies]
@@[Movies and Pictures, Movies]
AT THE START OF THE METHOD []
FFFFFFFFFFFFFFF
Current node is: name
Current nodedata is: Pictures
1[Pictures]
Current node is: file
Current nodedata is: name
1[Pictures]
Current node is: file
Current nodedata is: name
1[Pictures]
Current node is: file
Current nodedata is: name
1[Pictures]
Current node is: directory2.0
Current nodedata is: name
I found direct2.0
name of direct2.0 is Pictures2.0
0[Pictures]
0).5[Pictures, Pictures2.0]
1[Pictures, Pictures2.0]
!!!beforeoutput[Pictures, Pictures2.0]
!!!beforeoutput[Movies and Pictures, Movies, Pictures]
2 [Movies and Pictures, Movies, Pictures] // THIS IS THE OUTPUT
//I WANT THIS TO INCLUDE PICTURES2.0
///////////////////////
//// Example of a tree
//////////////////////
|-- directory
|-- name
|-- Movies and Pictures
|-- directory
|-- name
|-- Movies
|-- file
|-- name
|-- Harry Potter.mov
|-- date
|-- 20130602
|-- size
|-- 1400123
|-- file
|-- name
|-- Dodgeball.avi
|-- date
|-- 11111111
|-- size
|-- 5000000
|-- file
|-- name
|-- The Dark Knight.mpeg
|-- date
|-- 55555555
|-- size
|-- 7657657
|-- directory
|-- name
|-- Pictures
|-- file
|-- name
|-- Mountain.png
|-- date
|-- 20000000
|-- size
|-- 190000
|-- file
|-- name
|-- Flower.jpg
|-- date
|-- 140000
|-- size
|-- 7657657
|-- file
|-- name
|-- Wildlife.jpeg
|-- date
|-- 90000000
|-- size
|-- 250000
|-- directory2.0
|-- name
|-- Pictures2.0
示例树查询的解决方案如下:https://pastebin.com/FkBTatP6
答案 0 :(得分:0)
List<String> listofdirectories = new ArrayList<>();
在全球范围内宣布。
答案 1 :(得分:0)
您没有对返回值执行任何操作。看这里:
thenode.getDirectories();
好的,您已经调用了该方法,但是如何处理其返回值集?没有。你没有存储它。那么,如果你没有存储它,为什么你希望你的代码返回正确的集合呢?我认为这应该让你更接近问题的解决方案:
listofdirectories.addAll(thenode.getDirectories());