[英]Returning array from recursive method
Hello i hope you are all good.你好,我希望你一切都好。 I have this method i want to look recursively on a folder to count files and files that accept the condition and i want to return them as a array.我有这个方法我想递归地查看一个文件夹来计算接受条件的文件和文件,我想将它们作为数组返回。 I am having trouble on count+=countThem(file);我在 count+=countThem(file) 上遇到问题; , how can i fix it that that it can increase boll variables (count for files) and (countkusht for files that accept the condition) ,我该如何解决它可以增加 boll 变量(文件计数)和(接受条件的文件计数)
public int[] countThem(File f,String parameter) throws IOException {
int count = 0;
int countkusht = 0;
File[] files = f.listFiles();
for (File file : files) {
if(file.isDirectory()) {
//-> **count+=countThem(file);**
}
else if(!file.isDirectory()) {
count++;
fr = new FileReader(file);
br = new BufferedReader(fr);
String line = null;
while((line = br.readLine())!=null) {
if(line.contains(parameter)) {
countkusht++;
}
}
}
}
return new int[] {count,countkusht};
}
You need to index the array results of countThem您需要索引 countThem 的数组结果
if(file.isDirectory()) {
int[] ct = countThem(file, parameter);
count+=ct[0]
countkusht+=ct[1];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.