1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
emmainna [20.7K]
3 years ago
9

Write a method called swapPairs 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, then the next two, and so on. If the number of values in the list is odd, the method should not move the final element. For example, if the list initially stores ["to", "be", "or", "not", "to", "be", "hamlet"], your method should change the list's contents to ["be", "to", "not", "or", "be", "to", "hamlet"].
Computers and Technology
1 answer:
arsen [322]3 years ago
7 0

Answer:

  1. import java.util.ArrayList;
  2. public class Main {
  3.    public static void main(String[] args) {
  4.        ArrayList<String> strList =new ArrayList<String>();
  5.        strList.add("to");
  6.        strList.add("be");
  7.        strList.add("or");
  8.        strList.add("not");
  9.        strList.add("to");
  10.        strList.add("be");
  11.        strList.add("hamlet");
  12.        swapPairs(strList);
  13.        System.out.println(strList);
  14.    }
  15.    public static void swapPairs(ArrayList<String> list){
  16.        for(int i=0; i < list.size()-1; i+=2){
  17.            String temp = list.get(i);
  18.            list.set(i, list.get(i+1));
  19.            list.set(i+1, temp);
  20.        }
  21.    }
  22. }

Explanation:

Firstly, let's create a method swapPairs that take one ArrayList (Line 18). In the method, use a for-loop to traverse through each item in the ArrayList and swap the items between the current items at index-i and at index-i+1 (Line 19-22). The index-i is incremented by two in next loop and therefore the next swapping will proceed with third and fourth items and so forth.

In the main program, create a sample ArrayList (Line 5-12) and then test the method (Line 14) and print the output (Line 15). We shall get  [be, to, not, or, be, to, hamlet].

You might be interested in
The open items on your computer are displayed here.
koban [17]
The GUI or Graphical <u /><u></u><em />User Interface.
7 0
3 years ago
What is MS-Word? Write some versions of MS-Word.
shtirl [24]
Ms- word it is , hope it was helpful
8 0
3 years ago
Read 2 more answers
When selecting a color scheme for a project which two things should you consider?
Vsevolod [243]

colorfulness and depending on your teacher pick colors that will go together on the project

6 0
3 years ago
Why are there more producers then consumers?​
Ivanshal [37]

Answer:

because producer can make their own food

6 0
3 years ago
Read 2 more answers
Explain the difference between using the int type and the double type for numbers.
MaRussiya [10]
They have different semantics, <span>addition to having different semantics from </span>double<span>, </span>int<span> arithmetic is generally faster, and the smaller size (32 bits vs. 64 bits) leads to more efficient use of caches and data transfer bandwidth.....
Did That Help You In Anyway </span>
6 0
4 years ago
Other questions:
  • List 7 internal components in a computer system
    8·2 answers
  • Someone who participates online for social reasons usually has a:
    9·1 answer
  • Which of the following function headings arevalid? If they are invalid, explain why.
    12·1 answer
  • Sam needs to create a spreadsheet for his coworkers. They will need to follow a crossed a long road of data. Sam would like to m
    15·1 answer
  • When assigned to a cell, the __________ function returns a number that corresponds to the system date and time beginning with De
    12·1 answer
  • Sami is creating a web page for her dog walking business. Which item will set the theme for her page? Background color Heading c
    9·1 answer
  • In the digital age we have less time to absorb and make sense of new information
    12·2 answers
  • (1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output
    11·1 answer
  • Lasses give programmers the ability to define their own types. <br><br> a. True<br> b. False
    10·1 answer
  • How many levels of heading tags are allowed in html?
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!