Re: Code problem

From: Lāʻie Techie (laie_at_win_remove_get_nospam_solutions.com)
Date: 08/14/04


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



Relevant Pages

  • Re: Count all nodes in a treeview
    ... Which suggests that the TTreeNodes type exposes the entire tree as a flat hierarchy via its "Item" member. ... Pedro's code does this via recursion, returning for each node the sum of the number of direct descendants and the calculated value of nodes from each of those descendants. ... child I traverse the childnode and get all of it's settings too. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [patch] Real-Time Preemption, -RT-2.6.9-rc4-mm1-U8
    ... >>That's true for the case where the current priority is ... >>somewhere else handy and we don't need to traverse ... >>recursive acquisition of a mutex by a given task. ... where recursion checking would use a single owner field. ...
    (Linux-Kernel)
  • Re: Depth First Search traversal of this list
    ... I am trying to traverse the list C)) in a way that the ... works by iterating over the items at one level in the tree. ... are deferred into a queue, which is processed after the atoms are visited. ... After collecting both the queue and current level items, we do the recursion to ...
    (comp.lang.lisp)
  • Re: Regarding JTree
    ... Then you use recursion ... to traverse the tree. ... http://mindprod.com Again taking new Java programming contracts. ... Prev by Date: ...
    (comp.lang.java.gui)
  • Re: Code problem
    ... > It should traverse the folders and subfolders, but it stop at the first ... Your code portion is incomplete, so you leave us to guess. ... My guess is that the variable "files" is defined outside of traverse. ...
    (comp.lang.java.help)