|
. do not use recursion:
Import Java. io. file; import Java. util. optional list; public class filesystem {public static void main (string [] ARGs) {long a = system. currenttimemillis (); region list = new region list (); file dir = new file ("C: \ Java \"); file [] = dir. listfiles (); For (INT I = 0; I <file. length; I ++) {If (file [I]. isdirectory () list. add (file [I]); else system. out. println (file [I]. getabsolutepath ();} file TM P; while (! List. isempty () {TMP = List. removefirst (); If (TMP. isdirectory () {file = TMP. listfiles (); If (file = NULL) continue; For (INT I = 0; I <file. length; I ++) {If (file [I]. isdirectory () list. add (file [I]); else system. out. println (file [I]. getabsolutepath () ;}} else {system. out. println (TMP. getabsolutepath () ;}} system. out. println (system. currenttimemillis ()-);}} |
|
B. recursion:
Import Java. io. file; import Java. util. arraylist; public class filesystem1 {Private Static arraylist filelist = new arraylist (); public static void main (string [] ARGs) {long a = system. currenttimemillis (); refreshfilelist ("C: \ Java"); system. out. println (system. currenttimemillis ()-A);} public static void refreshfilelist (string strpath) {file dir = new file (strpath); file [] files = dir. listf Iles (); If (Files = NULL) return; For (INT I = 0; I
conclusion: Recursive Method B has better performance.