logo CodeStepByStep logo

switchPairs

Language/Type: Java ArrayList collections
Related Links:

Write a method named switchPairs that accepts an ArrayList of strings as a parameter and switches the order of pairs of values in the array. Your method should swap the order of the first two values, then switch the order of the next two, and so on. For example, if the array stores {"a", "bb", "c", "ddd", "ee", "f", "g"}, then the call of switchPairs(a); should switch the first pair ("a", "bb"), the second pair ("c", "ddd") and the third pair ("ee", "f"), to yield this list:

{"bb", "a", "ddd", "c", "ee", "f", "g"}

If there are an odd number of values, the last element is not moved.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.