The purpose of this assignment is to learn recursive methods. For this particular problem, I need to print the values of list
, one per line. The skeleton of the
When your doing recursion, it can sometimes be helpful to write out how you would perform the same task using a loop:
public void list(String[] list) {
for(int index = 0; index < list.length; index++) {
System.out.println(list[index]);
}
}
Now, say we wanted to get closer to a recursive solution. The first step might be to get rid of the interior part of the for
loop:
public void list(String[] list) {
for(int index = 0; index < list.length; index++) {
list(list, index);
}
}
public void list(String[] list, int index) {
System.out.println(list[index]);
}
Okay, now we are really close to the recursive solution. We just need to take the last two tasks that the loop is handling, incrementing index
and checking if index < list.length
. These look like they might be great candidates for a reduction step and a base case.
I think this is not the much good way even though it will be helpful.
static void traverse(int []x)
{
if(x.length == 0)
return;
traverse( Arrays.copyOf(x,x.length- 1));
System.out.println(x[x.length-1]);
}
Think of a recursive solution like this: given a bunch of things (like a list) do something with one of those things (like the first element of the list), then pass the rest of the things to the same solution that processed one of the things
Hint: you need to think about what your solution does in the case when it's given nothing.
Hint 2: Instead of "chopping up" the array each time you recurse, just note your place in the array.
If I were doing this I would use a helper method with the name of print()
or something similar.
In terms of thinking about the logic of the program, think about what you would need to do:
Have a helper method print()
that:
What do you think that second function could be? Hint: It's recursive...
Let's start from the beginning:
You probably already know that a recursive function is one that calls itself. If every time a function runs it calls itself, you are going to end up in an infinite loop. Therefore, it is important to always make sure your function knows when to stop calling itself as well.
How can we do this? The body of the function is not going to change between calls, since it is set at compile time. What will change, however, is the input to the function. Whenever you write a recursive function, you need to include logic that will avoid the recursive call when a certain condition is met.
In your example, the input to your function is an array of Strings. One way to know when to stop making the recursive call could be to pass a smaller array to the function each time, and stop when you have an empty array. This would work better in a language like C where arrays are exposed as pointers, but in Java it would be inefficient as you would have to create a new array each time.
A nicer solution would be to maintain an index pointing to your current position in the list (this is why you need a helper function - to add the extra parameter). At each call, you can simply print the String at the current index and increment the index. When the index is no longer inside the array, you're done!
If you really want to get a good understanding of recursion, I recommend learning a functional language such as Lisp, Haskell, or ML. Functional languages avoid mutable state (changing the values of variables), and as such they don't use things like for loops (because of the need to update a loop counter at every iteration). So they implement loops using recursion instead! For example, in Java we might have:
for (int i = 0; i < 10; i++) {
System.out.println("Loop index: " + i);
}
whereas in OCaml we would write the following:
let times = 10 in
let rec loop count =
if count = times then
printf "Loop index: %d" count ;
loop (count + 1)
;;
The important difference here is that, rather than changing the value of count (actually not possible given the above OCaml code), we pass a new value into the loop
function each time.
There are lots of good examples of recursion in Java. You would benefit greatly from reading these. First read your textbook, then continue with these examples