reverse3
Category: ArrayList
Author: Stuart Reges
Book Chapter: 10.1
Problem: reverse3
Write a static method called reverse3 that takes an
ArrayList of integer values as a parameter and that reverses each successive
sequence of three values in the list. For example, suppose that a variable
called list stores the following sequence of values:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4]
and we make the following call:
reverse3(list);
Afterwards the list should store the following sequence of values:
[19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204]
The first sequence of three values (3, 8, 19) has been reversed to be (19,
8, 3). The second sequence of three values (42, 7, 26) has been reversed to
be (26, 7, 42). And so on. If the list has extra values that are not part
of a sequence of three, those values are unchanged. For example, if the
list had instead stored:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2]
The result would have been:
[19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204, 99, 2]
Notice that the values (99, 2) are unchanged in position because they were
not part of a sequence of three values. 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