Answer:
The method is as follows:
public static void split(ArrayList<Integer> mylist) {
System.out.print("Before Split: ");
for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", "); }
System.out.println();
for (int elem = 0; elem < mylist.size(); elem+=2) {
int val = mylist.get(elem);
int right = val / 2;
int left = right;
if (val % 2 != 0) { left++; }
mylist.add(elem, left);
mylist.set(elem + 1, right); }
System.out.print("After Split: ");
for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", "); }
}
Explanation:
This declares the method
public static void split(ArrayList<Integer> mylist) {
This prints the arraylist before split
<em> System.out.print("Before Split: ");
</em>
<em> for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", "); }
</em>
<em> System.out.println();
</em>
This iterates through the list
for (int elem = 0; elem < mylist.size(); elem+=2) {
This gets the current list element
int val = mylist.get(elem);
This gets the right and left element
<em> int right = val / 2;
</em>
<em> int left = right;
</em>
If the list element is odd, this increases the list element by 1
if (val % 2 != 0) { left++; }
This adds the two numbers to the list
mylist.add(elem, left);
mylist.set(elem + 1, right); }
<em>This prints the arraylist after split</em><em>
</em>
<em> System.out.print("After Split: ");
</em>
<em> for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", "); }
</em>
<em>}</em>