logo Practice-It logo


Language/Type: Java Sets and Maps
Related Links:
Author: Marty Stepp (on 2021/04/02)

Write a method called extractOddIndexes that constructs and returns a new ArrayIntList of values that contains the sequence formed by removing the values at odd indexes in an existing ArrayIntList of values. For example, suppose that an ArrayIntList called list stores the following sequence of values:

        [13, 5, 7, 12, 42, 8, 23, 31]

If we make the following call on the method:

        ArrayIntList result = list.extractOddIndexes();

After the call, list and result would store the following:

        list  : [13, 7, 42, 23]
        result: [5, 12, 8, 31]

Notice that the values that were at odd indexes have been moved to the new ArrayIntList in the same order as in the original list and that the list now stores just values that were at even indexes also in the same order as in the original list. The list might have an odd number of values, as in:

        [14, -64, 16, 88, 21, 17, -93, 81, 17]

in which case the lists would store the following after the call:

        list  : [14, 16, 21, -93, 17]
        result: [-64, 88, 17, 81]

Notice that list stores five values while result stores only four. You are writing a method for the ArrayIntList class discussed in lecture:

        public class ArrayIntList {
            private int[] elementData; // list of integers
            private int size;          // current # of elements in the list


You may use the zero-argument constructor for ArrayIntList and you may assume that it will construct an array of sufficient capacity to store the result. If the original list is empty, the result should be an empty list.

You may call the ArrayIntList constructor, but otherwise you may not call any other methods of the ArrayIntList class to solve this problem. You are not allowed to define any auxiliary data structures other than the new ArrayIntList you are constructing (no array, String, ArrayList, etc). Your solution must run in O(n) time where n is the original list.

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

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.