0

Am working on some programming homework and am a bit lost. The project is to select the even/odd elements of a listarray and store in another array. It is not the even numbers in each element, but the elements themselves so if an array had values "1,2,5,7,9" and returned the even elements it would give "1, 5, 9". Also have to use recursion. Would anyone be able to give me a starting point or some advice. Though about starting with 2 elements and taking 2nd element and then building up from that, but don't know how it would add on the 2nd pass

 public static ArrayList<Integer> even(ArrayList<Integer> list)
 ArrayList<Integer> evenlist = ListMethods.deepClone(tList);//make copy of list
 if (evenlist.size()<=1)    // The list is empty or has one element
 {
//        return null;// Return the list as is
 }

if 
(evenlist.size()==2)
{
 //return right element
 //call method again
 //add to list
}
user1836661
  • 31
  • 2
  • 10

3 Answers3

2

Psuedocode

int[] evens,odds;

 function categorize(List<Integer> in,int idx)
   if(idx>=in.length)
         return
   int cur = in[idx]
   if(even), add to evens
   else add to odds
    categorize(in,idx+1)
smk
  • 5,340
  • 5
  • 27
  • 41
1

You could use a simple for loop like this:

for (int i = 0; i < list.size(); i += 2) {
    System.out.println(list.get(i));
}

If you have to use recursion, here's an outline of the steps you might take. (I won't tell you exactly what to do because you haven't tried anything and it is like homework.)

  1. Take first element and store it
  2. Remove (new) first element from list
  3. Call self
tckmn
  • 57,719
  • 27
  • 114
  • 156
1

This sounds similar to the homework I just completed, so if it is (And you're in my class!), I'll not tell you to use any terminology we haven't covered as I know it can be daunting trying to discover something new for practicals (beyond what we have to do).

First, set your exit condition. As you've already said, you have to create a new ArrayList out of the existing one. You are going to remove items from the existing ArrayList, storing the integers that are at even (or odd) indices, until the list is empty.

So your exit condition is:

if (evenList is Empty)
    return evenList;

Then, work your way through the steps. I would advise determining if the Array you start with has an even of odd number of steps, something like this:

if (evenList has Even Elements)
    int holderForIntsAtEvenElements = last evenList EVEN element

Note we start at the last element, so when you are coming OUT of the recursive method, this will be the last one added to your new ArrayList, and thus it'll be in numerical order. You might find this post interesting to do this: What does this boolean return mean?

We then want to remove the last element from the list and recursively call the method again.

Finally, when we hit our exit condition and start to come out, we want to add the ints we've been storing to them, e.g.:

evenList.add(holderForIntsAtEvenElements);
return evenList;

That doesn't solve one problem, which is what to do with the very first element if the list does NOT have an even number of elements - however, I'll let you try and solve that! That's a good mix of code and pseudo code and will hopefully help to get you on the right track.

Community
  • 1
  • 1
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92
  • Thanks Andrew. I am in your class (lets see if you can guess who). I think am just struggling applying recursion to alternative situations, as I do understand it in the letter notes and books. Might call for more practicals!!! – user1836661 Feb 17 '13 at 21:20
  • @user1836661: As things stand, your identity will forever remain a mystery! I'm not finding recursion all that easy myself! I'm not sure if you've finished this or not yet, but if not, try taking a look at this post - it was my original method of doing it: http://codereview.stackexchange.com/questions/22770/avoiding-use-of-an-initialized-variable-that-will-either-be-changed-or-ignored – Andrew Martin Feb 17 '13 at 21:22
  • I was also looking at it a different way, was going to select last 2 elements from array as went through recursion, take either the 1st or last number (odd/even) and add altogether at the end. ?? – user1836661 Feb 17 '13 at 21:25
  • You could certainly try it. One thing you should look into if you haven't used it before is the Eclipse Debugger - if you double click on the blue bar to the left of a line of code it will set a block point beside it. If you then choose to run the debugger (the bug icon), your program will stop at that line. Just push F6 to cycle through the program a line at a time then. It's very useful as you can see what will be stored in each data type and how your arraylists etc will change as the recursive method goes on. – Andrew Martin Feb 17 '13 at 21:27