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 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: -
/** call:
-
* findMatchOnDestination(destinationLocation, sourceFiles[i].getName())
-
*/
-
private void findMatchOnDestination(File f, String s){
-
try{
-
if (f.isDirectory()){ //recurse if directory
-
File[] flist = f.listFiles();
-
for (int i = 0; i < flist.length; ++i){
-
findMatchOnDestination(flist[i]);
-
}
-
}
-
else{ //file
-
if (matchesNameCriteria(f, s)){ //filter filename here
-
doSomethingOn(f);
-
}
-
}
-
}
-
catch(Exception e){
-
e.printStackTrace();
-
}
-
}
-
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: - /** call:
- * findMatchOnDestination(destinationLocation, sourceFiles[i].getName())
- */
- private void findMatchOnDestination(File f, String s){
- try{
- if (f.isDirectory()){ //recurse if directory
- File[] flist = f.listFiles();
- for (int i = 0; i < flist.length; ++i){
- findMatchOnDestination(flist[i]);
- }
- }
- else{ //file
- if (matchesNameCriteria(f, s)){ //filter filename here
- doSomethingOn(f);
- }
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
-
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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...
|
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;
|
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...
|
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...
|
by: Kay Schluehr |
last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
|
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...
|
by: athar.mirchi |
last post by:
..plz define it.
|
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 ??
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |