aspose file tools*
The moose likes Groovy and the fly likes Recursoin (groovy or java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Recursoin (groovy or java)" Watch "Recursoin (groovy or java)" New topic
Author

Recursoin (groovy or java)

Alireza Bahmanpour
Greenhorn

Joined: Oct 24, 2007
Posts: 23
Hi,
I have been told to write a recursive method to print the contents of a List. I believe that the psuedo code would look something like:


But I'm not quite sure how to implement it. Could someone help please.

I've been told to write it in groovy, but I don't mind if you could help me with its java version.

Thanks in advance
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I'm hard pressed to find a good reason to print the contents of a List (if we are talking java.util.List) via a recursive function when you can just iterate over the List with a for loop. It might help if you would define what kind of List you are talking about and what it contains.


GenRocket - Experts at Building Test Data
Alireza Bahmanpour
Greenhorn

Joined: Oct 24, 2007
Posts: 23
Well, in terms of Java, it is supposed to be an ArrayList of Strings.

To be honest, I can't find a good reason for it either.I know that the iterative method would be easy to write, but the problem here is that it is supposed to be recursive. It's an exam question, so I really need to figure out how to do it.

thanks
[ May 11, 2008: Message edited by: Alireza Bahmanpour ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Alireza Bahmanpour:
Well, in terms of Java, it is supposed to be an ArrayList of Strings.

To be honest, I can't find a good reason for it either.I know that the iterative method would be easy to write, but the problem here is that it is supposed to be recursive. It's an exam question, so I really need to figure out how to do it.

thanks

[ May 11, 2008: Message edited by: Alireza Bahmanpour ]


Well, we don't do homework here. What have you tried? We can help point you in the right direction.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

a .each iteration would be ridiculously simple in Groovy.

The recursion thing is really silly.

Anyway, your method of popping off the printed element would work. Another possibility is to include an indexing parameter to the method to keep track of where you're at.

Either way, you'll also need to test for an "I'm done" condition to stop the recursion.


A good workman is known by his tools.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Agreeing that it appears to be a hard way to do it, if "print the head of the list" means "print the 1st element", then "print the tail of the list" means "print the 2nd element, then the remaining tail." You just have to make sure you stop when you get to the end of the list. I hope that helps a bit.

I agree, it seems a hard way to go about things, but there are other programming paradigms, eg "functional" where everything is done by recursion. And (I think) recursion predates iteration, and when you do the theoretical basis underlying iteration you find it gives formulae identical to recursion.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I guess if you had a method that checked for the end of the list and returned on empty but called itself on !empty, you'd meet the requirements of the assignment. In which case, you can just use hasNext() as your terminal condition test.

Maybe the assigning instructor hates loops...


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursoin (groovy or java)