Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

evenOdd

Language/Type: Java arrays mod
Author: Helene Martin (on 2011/12/14)

Write a method named evenOdd that accepts an array of integers as a parameter and rearranges the array's elements so that all of its odd elements are in positions with odd-numbered indexes and all of its even elements are in positions with even-numbered indexes. The array passed in will always contain exactly as many even values as odd values. The exact order of the elements in the array after your method is run on it is unimportant, as long as its content alternates between even and odd values, starting with even. In other words, after your method is done, the array contents should follow the pattern [even value, odd value, even value, odd value...].

For example, if your method were called on the following array:

int[] a1 = {5, 6, 3, 3, 2, 5, 2, 6};
evenOdd(a1);

One acceptable ordering of the elements after the call would be:

[6, 5, 2, 3, 2, 5, 6, 3]

The even-numbered indexes of this array are 0, 2, 4, 6, and each of these positions contains an even integer. The odd-numbered indexes of this array are 1, 3, 5, 7, and each of these positions contains an odd integer.

You may assume that the array contains at least two elements. You may not create an additional array or use a String to solve this problem.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.