Is there any way to retain all of the values from each recursive call without using addAll? this is currently my code I've been struggling with this for hours.
//elements instantiation
public class RecursiveMethodsList<T extends Comparable<T>> {
private ArrayList<T> elements= new ArrayList<>();
the rest of my code:
private RecursiveMethodsList<T> retBetween( int index, T lowerValue,
T upperValue){
RecursiveMethodsList<T> list = new RecursiveMethodsList<T>();
if (index < elements.size()){
if (elements.get(index).compareTo(lowerValue) >= 0 &&
(elements.get(index).compareTo(upperValue)) <= 0){
list.elements.add(elements.get(index));
}
else retBetween(++index, lowerValue, upperValue);
}
return list;
}
basically what I'm doing is going through a list and if i come across a value that is greater than or equal to my lowerval parameter and less than or equal to my upperval parameter I add it to a new list and return that new list once it's finished
Have the List be one of your parameters to your recursive call like so:
private RecursiveMethodsList<T> retBetween(RecursiveMethodsList<T> list, int index, T lowerValue, T upperValue)
And when you first make the call to the recursive method just send it an empty List like so:
RecursiveMethodsList<T> returnedList = retBetween(new RecursiveMethodsList<T>(), int index, T lowerValue,
T upperValue)
Here is how I would structure the method to accomplish what you are trying to accomplish:
private RecursiveMethodsList<T> retBetween(int index, T lowerValue,
T upperValue){
if(index>=elements.size())return new RecursiveMethodsList<T>();
RecursiveMethodsList<T> lst = retBetween(index+1,lowerValue,upperValue);
if (elements.get(index).compareTo(lowerValue) >= 0 &&
(elements.get(index).compareTo(upperValue)) <= 0)
lst.elements.add(elements.get(index));
return lst;
}
I removed the RecursiveMethodsList<T> from the parameters because after re-reading your question I got a better understanding of what exactly you were trying to do.
So essentially, the first line in the method is your base case this is important in recursion, so that it doesn't continue to recurse forever. This will call it self again and again until it calls it self with an index outside the bounds of elements and then return a new RecursiveMethodsList.
Then, it will start doing test backwards through the list and adding elements to the list that is being returned until it has worked its way through the entire elements list
This can also be done with Tail Recursion, which is a more memory efficient because if done properly, will release the stack frame of each method call at the time of call like so:
private RecursiveMethodsList<T> retBetween(RecursiveMethodsList<T> list, int index, T lowerValue, T upperValue){
if(index>=elements.size())return list;
if (elements.get(index).compareTo(lowerValue) >= 0 &&
(elements.get(index).compareTo(upperValue)) <= 0)
list.elements.add(elements.get(index));
return retBetween(list, index+1, lowerValue, upperValue);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With