非再帰ウォークスルーファイルシステムを使用し、特定の深さにあるファイルをプリントするアプリケーションを作成する必要があります。 私が持っているもの:Java非再帰的ファイルシステムウォーク
public void putFileToQueue() throws IOException, InterruptedException {
File root = new File(rootPath).getAbsoluteFile();
checkFile(root, depth);
Queue<DepthControl> queue = new ArrayDeque<DepthControl>();
DepthControl e = new DepthControl(0, root);
do {
root = e.getFileName();
if (root.isDirectory()) {
File[] files = root.listFiles();
if (files != null)
for (File file : files) {
if (e.getDepth() + 1 <= depth && file.isDirectory()) {
queue.offer(new DepthControl(e.getDepth() + 1,file));
}
if (file.getName().contains(mask)) {
if (e.getDepth() == depth) {
System.out.println(Thread.currentThread().getName()
+ " putting in queue: "
+ file.getAbsolutePath());
}
}
}
}
e = queue.poll();
} while (e != null);
}
とヘルパークラス
public class DepthControl {
private int depth;
private File file;
public DepthControl(int depth, File file) {
this.depth = depth;
this.file = file;
}
public File getFileName() {
return file;
}
public int getDepth() {
return depth;
}
}
私はこのプログラムが原因幅優先探索(右の翻訳を願っています)の追加メモリを使用していること、答えを受けました。私はO(k^n)を持ちます。ここで、k - サブディレクトリの平均量、n - 深さ。このプログラムはO(k * n)で簡単に実行できます。私のアルゴリズムを修正するのを助けてください。
なぜ非再帰的である必要がありますか? –
@HotLicks、それはおそらく宿題関連の要件です。 – Moonbeam
ちょうど合併症のために、私は思う。面接の仕事です。 – user1255246