• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Recursoin (groovy or java)

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alireza Bahmanpour
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 76453
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bras cause cancer. And tiny ads:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic