By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

recursion blue

P: n/a
Hi

I have a following recursive method:

private File findMatchOnDestination(File[] fileList, String name) {
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
System.out.println("file: " + fileList[i]);
File[] matches = fileList[i].listFiles(new MatchingNameFilter(name));

if (matches != null && matches.length > 0) {
searchResult = matches[0];
return matches[0];
}
if (searchResult == null) {
findMatchOnDestination(fileList[i].listFiles(), name);
} else {
return searchResult;
}
}
}

return null;
}

and this is how it is inovked:
File[] destFiles = destinationLocation.listFiles(new
SourceFoldersFilter(getFilterFolders()));

for (int i = 0; i < sourceFiles.length; i++) {
File destFile =
findMatchOnDestination(destFiles,sourceFiles[i].getName());
// do something
}

So essentially, I pass a filtered list of files in the destinationLocation,
upon which the findMatchOnDestination() works.

However, as you'd agree the code in findMatchOnDestination() is ugly -
especially the use of "searchResult" static member. Can anyone help me tidy
the code?

Thanks
Manav
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Manavendra Gupta" <ma*********@cramer.com> wrote in message news:<40***********************@reading.news.pipex .net>...
Hi

I have a following recursive method:

private File findMatchOnDestination(File[] fileList, String name) {
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
System.out.println("file: " + fileList[i]);
File[] matches = fileList[i].listFiles(new MatchingNameFilter(name));

if (matches != null && matches.length > 0) {
searchResult = matches[0];
return matches[0];
}
if (searchResult == null) {
findMatchOnDestination(fileList[i].listFiles(), name);
} else {
return searchResult;
}
}
}

return null;
}

and this is how it is inovked:
File[] destFiles = destinationLocation.listFiles(new
SourceFoldersFilter(getFilterFolders()));

for (int i = 0; i < sourceFiles.length; i++) {
File destFile =
findMatchOnDestination(destFiles,sourceFiles[i].getName());
// do something
}

So essentially, I pass a filtered list of files in the destinationLocation,
upon which the findMatchOnDestination() works.

However, as you'd agree the code in findMatchOnDestination() is ugly -
especially the use of "searchResult" static member. Can anyone help me tidy
the code?

Thanks
Manav


Think simple and do simple:
Expand|Select|Wrap|Line Numbers
  1. /** call:
  2. * findMatchOnDestination(destinationLocation, sourceFiles[i].getName())
  3. */
  4. private void findMatchOnDestination(File f, String s){
  5. try{
  6. if (f.isDirectory()){ //recurse if directory
  7. File[] flist = f.listFiles();
  8. for (int i = 0; i < flist.length; ++i){
  9. findMatchOnDestination(flist[i]);
  10. }
  11. }
  12. else{ //file
  13. if (matchesNameCriteria(f, s)){ //filter filename here
  14. doSomethingOn(f);
  15. }
  16. }
  17. }
  18. catch(Exception e){
  19. e.printStackTrace();
  20. }
  21. }
  22.  
Jul 17 '05 #2

P: n/a
aah, didnt think of that - was keen to return a value from the recursive
method. Thanks!
"hiwa" <HG******@nifty.ne.jp> wrote in message
news:68**************************@posting.google.c om...
"Manavendra Gupta" <ma*********@cramer.com> wrote in message

news:<40***********************@reading.news.pipex .net>...
Hi

I have a following recursive method:

private File findMatchOnDestination(File[] fileList, String name) {
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
System.out.println("file: " + fileList[i]);
File[] matches = fileList[i].listFiles(new MatchingNameFilter(name));
if (matches != null && matches.length > 0) {
searchResult = matches[0];
return matches[0];
}
if (searchResult == null) {
findMatchOnDestination(fileList[i].listFiles(), name);
} else {
return searchResult;
}
}
}

return null;
}

and this is how it is inovked:
File[] destFiles = destinationLocation.listFiles(new
SourceFoldersFilter(getFilterFolders()));

for (int i = 0; i < sourceFiles.length; i++) {
File destFile =
findMatchOnDestination(destFiles,sourceFiles[i].getName());
// do something
}

So essentially, I pass a filtered list of files in the destinationLocation, upon which the findMatchOnDestination() works.

However, as you'd agree the code in findMatchOnDestination() is ugly -
especially the use of "searchResult" static member. Can anyone help me tidy the code?

Thanks
Manav


Think simple and do simple:
Expand|Select|Wrap|Line Numbers
  1.  /** call:
  2.    * findMatchOnDestination(destinationLocation, sourceFiles[i].getName())
  3.    */
  4.  private void findMatchOnDestination(File f, String s){
  5.    try{
  6.      if (f.isDirectory()){ //recurse if directory
  7.        File[] flist = f.listFiles();
  8.          for (int i = 0; i < flist.length; ++i){
  9.            findMatchOnDestination(flist[i]);
  10.        }
  11.      }
  12.      else{ //file
  13.        if (matchesNameCriteria(f, s)){ //filter filename here
  14.          doSomethingOn(f);
  15.        }
  16.      }
  17.    }
  18.    catch(Exception e){
  19.      e.printStackTrace();
  20.    }
  21.  }
  22.  

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.