Question
Write a method switchPairs that switches the order of values in an ArrayList of strings in a pairwise fashion. Your method should switch the order
Write a method switchPairs that switches the order of values in an ArrayList of strings in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the list initially stores these values: ["four", "score", "and", "seven", "years", "ago"} Your method should switch the first pair, "four" and "score", the second pair, "and" and "seven", and the third pair, "years", "ago", to yield this list: ["score", "four", "seven", "and", "ago", "years"] If there are an odd number of values, the final element is not moved. For example, if the list had been: ["to", "be", "or", "not", "to", "be", "hamlet"] It would again switch pairs of values, but the final value "hamlet" would not be moved, yielding this list: ["be", "to", "not", "or", "be", "to", "hamlet"]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started