Array Simulation
Category: Simulation
Author: Stuart Reges
Book Chapter: 7.1
Problem: Array Simulation
You are to simulate the execution of a method
that manipulates an array of integers. Consider the following method:
public static void mystery(int[] list) {
for (int i = 1; i < list.length; i++) {
if (list[i - 1] % 2 == 0) {
list[i - 1]++;
list[i]++;
}
}
}
In the left-hand column below are specific lists of integers. You are to
indicate in the right-hand column what values would be stored in the list
after method mystery executes if the integer list in the left-hand column
is passed as a parameter to mystery.
Original List Final List
------------------------------------------------------------
{12, 7} ____________________________
{2, 3, 4, 5, 6} ____________________________
{3, 4, 5, 7, 9} ____________________________
{2, 3, 5, 7, 9} ____________________________
{4, 5, 9, 6, 2} ____________________________
1) [12, 7]
2) [2, 3, 4, 5, 6]
3) [3, 4, 5, 7, 9]
4) [2, 3, 5, 7, 9]
5) [4, 5, 9, 6, 2]