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
~ I already know this ~ who was the bits victim in five nights at freddys 4 <br> ___10 points___
Andreas93 [3]

Answer:

i dont know anything about them only that the bear is the ducks son and the duck is the fox mom.

Explanation:

6 0
3 years ago
Read 2 more answers
URLs are the global ______ of resources on the Internet.
adell [148]
I believe the answer might be addresses 
 <span />
4 0
3 years ago
Read 2 more answers
Which of the following is another term for a subfolder?
LenKa [72]
Subdirectory

Have a great day! c:
5 0
2 years ago
File names should describe what is in the file in a few words
JulijaS [17]

Answer

File names should describe the content of the file.

Explanation

File names are set of words  which are used to uniquely identify computer files which are stored in a file system. This helps one to know the contents of the file which you want to find. When you name these file names you use necessary characters you use descriptive words so that you dont have hard times when searching for them. The naming also is determined by the file system you are using because different systems impose different restrictions on the length of the file names and the allowed characters within file names.

3 0
3 years ago
Meg wants to preview all images in a folder so that she can quickly find the image she wants. Which view will help her do this?
Liono4ka [1.6K]
Thumbnails is the answer

3 0
3 years ago
Other questions:
  • Jason works as an accountant in a department store. He needs to keep a daily record of all the invoices issued by the store. Whi
    13·2 answers
  • Your traffic light changes to yellow as you approach an intersection. In most cases, what action should you take?
    15·2 answers
  • A. Draw a flowchart or write pseudocode to represent the logic of a program that allows the user to enter an hourly pay rate and
    8·1 answer
  • How does the text recommend that a company operate if it wants to be successful in the information age?
    11·1 answer
  • Which best describes obliteration in a forged document?
    11·1 answer
  • What 2 important components are found on a motherboard? (2 points each)
    13·1 answer
  • How to format the selected range of cells as u.s currency
    6·1 answer
  • A data center needs to ensure that data is not lost at the system level in the event of a blackout. Servers must stay operable f
    6·1 answer
  • Describe at least one issue of terrorism that has happened recently.
    14·1 answer
  • PLS HELP SOON
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!