File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A little help with recursion? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A little help with recursion?" Watch "A little help with recursion?" New topic
Author

A little help with recursion?

Natalie Chen
Greenhorn

Joined: Feb 03, 2008
Posts: 1
Hi guys!

Say I have the following method:


I want to make it so that when given "abc" it returns in an ArrayList:
"abc"
"ab"
"a"
""

Right now, I'm trying to do this recursion alone. I've come to sort of have the idea that it's repeating the second line and throwing everything off. Is there any way to fix this to make it work as intended, or do I have a faulty plan and need to rewrite the whole thing?

Bwuh. So much frustration over simple code.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

// Ok lets see how does your code work

let assume that you call your code by

stripLast("abc")

creating list --->s.length() == 3 ---> inside else ---> list.add(s.substring(0,2)) ---> list.add("abc")

Now you should call your method again with parameter "ab", in order to make it work properly..!!!

stripLast(s.length() - 1) ---> stripLast(3 - 1) ---> stripLast(2) ---> ??? (This is the first problem)

After correcting this problem lets see what is the other problem:

stripLast("ab")

creating list --->s.length() == 2 ---> inside else ---> list.add(s.substring(0,1)) ---> list.add("ab")

Wait..Wait Wait..!! Whats this..we are creating the list again..so earlier list will be wiped off..!! Everytime the method is called a new list is created and earlier one lost its existence...!! so instead of that
we should get the earlier list and add this string..!!
one possible way is to define the list outside the method..and its scope should be such that ..it is accessible with in the method..!!

Last thing to take care of..See the method is static..!!!javascript: x()
jumpingjoy javascript: x()
beerchug


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jilesh Lakhani
Ranch Hand

Joined: Jul 26, 2006
Posts: 49



Issue 1:
Every Time you call the function stripLast a new arrayList is created, I hope you are getting my point, this should be static and out side this class

Issue 2:
stripLast accepts String as the input parameter and not Integer as the Input Param.

Once you've made the changes.. you will receive your desired output.


-Jilesh
Universe and Knowledge has no bounderies
Jilesh Lakhani
Ranch Hand

Joined: Jul 26, 2006
Posts: 49



Issue 1:
Every Time you call the function stripLast a new arrayList is created, I hope you are getting my point, this should be static and out side this class

Issue 2:
stripLast accepts String as the input parameter and not Integer as the Input Param.

Once you've made the changes.. you will receive your desired output.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A little help with recursion?