473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

recursion blue

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
2 1621
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.