split
Category: ArrayList
Author: Stuart Reges
Book Chapter: 10.1
Problem: split
Write a static method called split that takes an
ArrayList of integer values as a parameter and that replaces each value in
the list with a pair of values, each half the original. If a number in the
original list is odd, then the first number in the new 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]
The number 18 is split into the pair (9, 9), the number 7 is split into (4,
3), the number 4 is split into (2, 2), the number 24 is split into (12, 12)
and the number 11 is split into (6, 5). Thus, the call:
split(list);
should cause list to store the following sequence of values afterwards:
[9, 9, 4, 3, 2, 2, 12, 12, 6, 5]
You may assume that all numbers in the list are nonnegative. 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