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

reverse3

Language/Type: Java ArrayList
Author: Marty Stepp (on 2020/04/16)

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. You may not construct any extra data structures to solve this problem. You must solve it by manipulating the ArrayList you are passed as a parameter. See the cheat sheet for a list of available ArrayList methods.

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.