-1

The question is rather simple, but my brain boils :((( I know, that i should recursive function, but I can't organize it properely, because I'm limited by the types(it's a requirment)

public String[] getFiles(String initialDir) {}

First in my code I get the list of files and directories of initialDir then i go through the elements and look up for directories with the help of

.isDirectory()

and then I try to organize recursion, something like:

String [] buf = getFiles(Filelist[i].toString());

and here come troubles.

The main problem is, determining the size of obtained data and allocating the necessary array of String [] type and further writing all recursion results and first-call results to the resulting array of String [] type.

It really annoys me, so i need a fresh look from a side (maybe someone will help with code in the function).

Thanks to all!

bezmax
  • 25,562
  • 10
  • 53
  • 84
Helgus
  • 177
  • 3
  • 7
  • 17
  • 2
    Do you have to return `String[]`, could you instead return `List`? That way you don't have to know up-front how many items you're going to require. –  Feb 24 '12 at 11:46
  • Possible duplicate: http://stackoverflow.com/questions/2056221/recursively-list-files-in-java – assylias Feb 24 '12 at 11:49
  • @Peter, no i can't return List, if i could, the solution was much simpler – Helgus Feb 24 '12 at 11:51
  • 1
    Are you allowed to use `ArrayList` or other types of collections internally? This is homework, right? – Joni Feb 24 '12 at 11:51
  • @Helgus You can't use List AT ALL, or only return it? Also, can you create a different method which will return List? – bezmax Feb 24 '12 at 11:54

2 Answers2

3

You can use a list in the body of your method then convert it to an array:

public String[] getFiles(String initialDir) {
   List<String> files = new ArrayList<String>();
   ...
       files.add(aLovelyFile);
   ...
       String[] moarFiles = getFiles(aLovelyDirectory);
       files.addAll(Arrays.asList(moarFiles));
   ...
   return files.toArray(new String[0]);
}
  • thank you a LOT!!!!! i've tried to use collection, but then i lost in me own code and indexes... – Helgus Feb 24 '12 at 12:09
1

If this isn't homework, look at Apache Commons, specifically FileUtils#ListFiles:

Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter. If your search should recurse into subdirectories you can pass in an IOFileFilter for directories. You don't need to bind a DirectoryFileFilter (via logical AND) to this filter. This method does that for you.

An example: If you want to search through all directories called "temp" you pass in FileFilterUtils.NameFileFilter("temp")

This returns a Collection<File>. Probably exactly what you want. Of course, if you need to return an array, use .toArray() at the end.

If you really really have to use arrays, and it's still not homework, then you can concatenate two arrays, using Apache commons lang:

String[] both = (String[]) ArrayUtils.addAll(first, second);

The above was copied from the answer to How to concatenate two arrays in Java?, which gives you all the answers you'll ever want.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • I really do not think anyone should reinventing these kind of utilities. So, plus 1 to the suggestion of using FileUtils. Of course, the homework cavet withstanding. :) – Pavan Feb 24 '12 at 11:56