# Recursive thinking

Steve Haggan
Greenhorn
Posts: 12
I have a problem where I need to make an Arraylist of numbers recursively.
I then need use recursion to create another ArrayList of the elements at the even indexes of the arraylist.
My output appears to be at the mercy of my input.
If i put in an even number lets say 10 - the output is 2,4,6,8,10 (and i know these are not the even indexes of the array!)
However if I put an odd number in - 9, the output is 1,3,5,7,9 (which is the correct even indexes of the array.)
Im sure its something simple but i cannot see where I have gone wrong.
Code attached

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Steve Haggan wrote:
If i put in an even number lets say 10 - the output is 2,4,6,8,10 (and i know these are not the even indexes of the array!)
However if I put an odd number in - 9, the output is 1,3,5,7,9 (which is the correct even indexes of the array.)

I dont understand your code. *just guessing*, your logic can get some light here..

1. just think if i pass n to method say print(n) then it should print 1 2... n recursively

2. now how can I trace even or odd so that i can print all even and all odd , *is this necessary* ? just subtract 2 while traveling recursively ...

Steve Haggan
Greenhorn
Posts: 12
Seetharaman Venkatasamy wrote:
Steve Haggan wrote:
If i put in an even number lets say 10 - the output is 2,4,6,8,10 (and i know these are not the even indexes of the array!)
However if I put an odd number in - 9, the output is 1,3,5,7,9 (which is the correct even indexes of the array.)

I dont understand your code. *just guessing*, your logic can get some light here..

1. just think if i pass n to method say print(n) then it should print 1 2... n recursively

2. now how can I trace even or odd so that i can print all even and all odd , *is this necessary* ? just subtract 2 while traveling recursively ...

Im not sure if I understand what you are saying, or perhaps I have explained it badly!
My task is to print out all the even element numbers
ie if n was 6 the initial array is 1,2,3,4,5,6 thus ArrayList (0) as an even number would print out 1, ArrayList (2) as an even number would print 3 and arrayList (4) as an even number will print out 5.
However with my code if n = 6 the out put is 2,4,6
But if my input is changed to 5 it prints out 1,3,5
I dont understand how the value of n can have an impact upon my recursive method to find all the values at an even element of the array.

Campbell Ritchie
Sheriff
Posts: 51368
87
There is something wrong about your approach, and I can’t quite put my finger on it. There are a few specific things: for example, you have completely misunderstood what the isEmpty() method does. You do not need to use that technique to copy a List; you can use one of its constructors.
Why have you got all those static methods? Are you actually using object‑orientation.
I am finding the point of the exercise hard to understand (like Seetharaman). Are you supposed to remove odd/even‑placed elements from the List? And your method to remove alternate elements is really difficult to understand (even without the recursion).
I suggest you draw the structure of a List, like this… and examine the structure of the List as you go through that even method.
There is something wrong aboutYou can simply write