标准的JDK里面只有栈(java.util.Stack),而没有队列,所以在使用栈来实现遍历时,进行深度遍历,如果实在想实现广度遍历的话,可以自己组合java.util.LinkedList来实现一个队列。
import
java.io.File;
import java.util.Stack;
public class NoRecursive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File root = new File("C:/");
Stack stack = new Stack();
stack.push(root);
while(stack.size()>0){
File file = (File)stack.pop();
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory() && !files[i].isHidden()){
stack.push(files[i]);
}else if(files[i].isFile() && !files[i].isHidden()){
System.out.println(files[i].getPath());
}
}
}
}
}
import java.util.Stack;
public class NoRecursive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File root = new File("C:/");
Stack stack = new Stack();
stack.push(root);
while(stack.size()>0){
File file = (File)stack.pop();
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isDirectory() && !files[i].isHidden()){
stack.push(files[i]);
}else if(files[i].isFile() && !files[i].isHidden()){
System.out.println(files[i].getPath());
}
}
}
}
}