[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:What if the base was "this file is not a directory"? Then you recurse one level lower than now, but don't need a loop.
Paul Clapham wrote:So you want to find out whether there are files in a directory? Well, let's take the case where there are (say) 23 entries returned by the File.listFiles() method. Now, some of those 23 entries may be directories and some may be files, i.e. not directories. I can't think of a way to find out whether any of them are files without looking at each of the 23 entries. I'm not sure why you want to avoid the loop, anyway.
Junilu Lacar wrote:Recursive list processing without loops is very common in functional programming. Basically, you keep slicing the list into two parts: the head element and the rest of the list, aka the tail. You process the head and then keep recursing with the tail until the next tail is empty.
Carey Brown wrote:Usually a recursive method to visit all files in a tree incorporates a loop somewhere in the method. Although you could, as you say, recursively slice a list into parts, that won't work when the only parameter you're given is a File, not a List.
Junilu Lacar wrote:
Carey Brown wrote:Usually a recursive method to visit all files in a tree incorporates a loop somewhere in the method. Although you could, as you say, recursively slice a list into parts, that won't work when the only parameter you're given is a File, not a List.
If the only parameter you are given is a file, how do you propose to loop through it?
Junilu Lacar wrote:So if the file is a directory, you get a *list* of files in it... Now you're back to splitting the list into two parts and recursing without a loop
Junilu Lacar wrote:So if the file is a directory, you get a *list* of files in it... Now you're back to splitting the list into two parts and recursing without a loop
Junilu Lacar wrote:On second thought, scratch the idea of two methods. You just need a method that takes List<File>. Base case is when list.size() is 0 or 1 and the File, if there is one, is not a directory.
Junilu Lacar wrote:A couple of questions:
1. What is the significance of the long return value?
2. Is the parameter list constraint to what you have?
The first question has to do with intent. What is the returned value supposed to represent?
The second question pertains to Carey's reply where he shows a slightly different method signature. I don't see any way for you to arrive at a correct solution with just a File parameter like what you have now.
Carey Brown wrote:Naziru, I think you need to take a step back from the problem. What if you were asked to print an array of Strings using recursion instead of a loop?
If you have followed the earlier posts you'd see that this only works with variable length arguments, as in String... strings. The reason that this is necessary is that the recursive algorithm depends on the ability to operate on an array. Line 6 is already an array so it wouldn't have been an issue. Line 3 however is not written as an array, BUT, thanks to the miracle of variable length arguments, the compiler turns it into an array on our behalf. On line 10 strings is passed to the body of the method as a String[] array.
Line 18 uses copyOfRange() to generate a new array without the first entry, thereby reducing the length of the array by one.
Carey Brown wrote:Naziru, I've read through the requirements that you PM'd me and I do think you've misread the instructions. They were talking about using a loop for the tree depth which is different than using a loop to go through all the children at a given position. This makes the problem much simpler. See my last post.