119k views
19 votes
BJP4 Self-Check 7.21: swapPairs

Write a method named swapPairs that accepts an array of strings as a parameter and switches the order of values 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.

PLEASE HELP DUE AT 11:59

User NKognito
by
2.9k points

1 Answer

5 votes

Answer:

public static void swapPairs(ArrayList strList)

{

for(int i = 0; i < strList.size() - 1; i += 2)

{

String temp1 = strList.get(i);

String temp2 = strList.get(i + 1);

strList.set(i, temp2);

strList.set(i + 1, temp1);

}

}

Step-by-step explanation:

User Phisn
by
3.0k points