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

stretch

Language/Type: Java ArrayList Collections
Author: Stuart Reges (on 2014/02/13)

Write a method stretch that doubles the size of an ArrayList of integers by replacing every integer in the list with a pair of integers, each half the original. If a number in the original list is odd, then the first number in the replacement pair should be one higher than the second so that the sum equals the original number. For example, if a variable called list stores this sequence of values:

        [18, 7, 4, 24, 11]

and you make the following call:

        stretch(list);

Then the number 18 is stretched into the pair (9, 9), the number 7 is stretched into (4, 3), the number 4 is stretched into (2, 2), the number 24 is stretched into (12, 12) and the number 11 is stretched into (6, 5) yielding the following overall list which is twice the length of the original list:

         [9, 9, 4, 3, 2, 2, 12, 12, 6, 5]

Recall that the primary methods for manipulating an ArrayList are:

         add(E value)               appends value at end of list
         add(int index, E value)    inserts given value at given index, shifting
                                    subsequent values right
         clear()                    removes all elements of the list
         get(int index)             returns the value at given index
         remove(int index)          removes and returns value at given index,
                                    shifting subsequent values left
         set(int index, E value)    replaces value at given index with given value
         size()                     returns the number of elements in list

Write your solution to stretch below.

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.