Re: Code problem
From: Lāʻie Techie (laie_at_win_remove_get_nospam_solutions.com)
Date: 08/14/04
- Next message: Andrew Thompson: "Re: finding input files (user input)"
- Previous message: Zoltan Sekeres: "Re: Adding a native method to String"
- In reply to: Ian Shef: "Re: Code problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 14 Aug 2004 11:37:57 -0600
On Thu, 12 Aug 2004 19:02:21 +0000, Ian Shef wrote:
> jacov <jacov@katamail.com> wrote in
> news:cffnnk$hum$2@lacerta.tiscalinet.it:
>
>> HI all
>> I have a problem whit this portion of code
>>
>> public static java.util.ArrayList traverse(File rootDir,FileFilter
>> filter) {
>> if (filter!=null)
>> files = rootDir.listFiles(filter);
>> else
>> files = rootDir.listFiles();
>>
>> for( int i = 0; i<files.length; i++) {
>> File f = new File(rootDir,files[i].getName()); //File f =
>> files[i];
>> //System.out.println (f.getAbsolutePath()); if
>> (f.isDirectory()) {
>> traverse(f,filter);
>> }
>> }
>> } (don't think about parens...)
>> It should traverse the folders and subfolders, but it stop at the first
>> Folder. Why
>> I hope you can help me...
> Your code portion is incomplete (won't compile), so you leave us to guess.
> My guess is that the variable "files" is defined outside of traverse.
> Thus, when traverse call itself, the previous value of files is wiped out.
> files should be defined locally to traverse.
Here are a few things I noticed:
The method should return List, not ArrayList. By moving to an interface
the programmer is free to change implementation details more easily.
The OP also forgets to add the results of the recursive call to the the
List which is returned.
Since this is recursive, you may want to pass in a List as a third
parameter. If the List is null, create a new ArrayList (or whatever
implementation of List that suits you best). Return this List. This
little detail only creates a single List no matter how deep the recursion
is.
HTH,
La'ie Techie
- Next message: Andrew Thompson: "Re: finding input files (user input)"
- Previous message: Zoltan Sekeres: "Re: Adding a native method to String"
- In reply to: Ian Shef: "Re: Code problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|