![]() |
| ||
| Recursion Problem public static boolean member(Integer obj,SimpleList<Integer> l); the question is how could I use recursion to show that there is a member of a list? I thought that maybe if (l.isEmpty()) {return false} else ? thanks |
| ||
| Re: Recursion Problem Could you detail a bit more. |
| ||
| Re: Recursion Problem Quote:
public class SimpleList<E> {What I have so far is:public static boolean member(Integer obj,SimpleList<Integer> l);I figured since if the list is empty then it would not have obj as a member. However Im having a hard time trying to understand the logic of this and how to use it in recursion. |
| ||
| Re: Recursion Problem Generally when you are doing recursion on any kind of collection of items (lists, arrays, even strings) you want to solve the problem for the first element of the collection, then recurse on the remaining part of the collection. For this example you can look at the first element of the list, check if that is the object you are looking for. If it is, then you are done. If it's not, you recurse on the rest of the list (without that first element). Now, because your function is only taking the list and search object as a parameter, you have no way of sending a portion of a list to recurse on. What you will have to do is create a second recursive function (often called the auxiliary recursive function) that takes an additional parameter of the location in the list to start searching from. When someone calls your original function, you just call the auxiliary function with the location as 0. Try coding that up and if you have problems, post both functions. |
| ||
| Re: Recursion Problem That really helped my thought process. I figured it out! lol took me a while but i finally got what you where saying. I did it with a try catch block and it worked out perfectly. Thanks again! |
| All times are GMT -4. The time now is 10:50 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC