logo Practice-It logo


Related Links:
Author: Stuart Reges (on 2014/02/13)

Write a method reverseCopy that returns a new ArrayIntList that contains a copy of the values in the original list in reverse order. For example, if a variable list stores the following sequence of values:

        [17, 42, 3, 8, 9, 12]

and the following call is made:

        ArrayIntList list2 = list.reverseCopy();

Then the variable list2 should store the following sequence of values:

        [12, 9, 8, 3, 42, 17]

The original list should not be changed by the method. The new list should have the same capacity as the original. Remember that there is a constructor for ArrayIntList that takes a capacity as a parameter:

        // pre : capacity >= 0
        // post: constructs an empty list with the given capacity
        public ArrayIntList(int capacity)

If the original list is empty, the result should be an empty list. 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 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 call methods from the Arrays class like the copyOf method. Your solution must run in O(n) time where n is the length of the 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.