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
Which design element is used to convey smoothness or roughness in a design
iris [78.8K]

Keyboard would be the correct answer

5 0
3 years ago
Following the car in front of you at a closer distance _____.
tiny-mole [99]

Answer:

B. Doesn’t get you to your location faster.

Explanation:

Following the car in front of you at a closer distance doesn’t get you to your location faster.

6 0
3 years ago
Read 2 more answers
Renee works for a television series. Her responsibility is to transfer data from the camera to a hard drive. What is her job des
Marysya12 [62]

Answer:

Data wrangler

Explanation:

Her responsibility is to transfer data from the camera to a hard drive. What is her job designation? Data Wrangler.

<h2>mark as brainliests </h2>
8 0
2 years ago
I need help pls, in this code if add row for example :
tankabanditka [31]
I thing is going project experiment everyone make plans and
3 0
3 years ago
When using _____, developers are required to comply with the rules defined in a framework. (Points : 2) inheritance
Nikolay [14]

Answer: Contracts

Explanation:

The contract is the mechanism which is basically used by the developers to follow the set of rules that is defined in the framework with the proper specification in the API ( Application programming interface).

The contract is the proper agreement between the two and more developers so that they must follow the rules that is mentioned in the agreement contract.  

The contract is widely used in the software development process in which all the possible design requirement are mentioned according to the needs of the client.

Therefore, Contract is the correct option.

7 0
3 years ago
Other questions:
  • What needs to be increased in order to increase image size and clarity?
    10·1 answer
  • The person in charge of recording the sound should always
    15·1 answer
  • g If a class named Student has a data member named gpa , and one of its member functions has a parameter also named gpa , how ca
    7·1 answer
  • Which is a circuit board located behind an LCD screen on a laptop?
    7·1 answer
  • Justine was interested in learning how to play the piano. Before she was allowed to even play the piano, she has had to learn
    15·2 answers
  • If the algorithm does not have instructions for unanticipated results, the computer program will a. solve a problem b. start ove
    6·1 answer
  • Please Help Me!!!!!!!!!!!!!
    5·1 answer
  • Which of the following is not the disadvantage of closed
    7·2 answers
  • Which native windows application allows you to access basic pc settings and controls such as system information, controlling use
    7·1 answer
  • Write a program to demonstrate circular linked list with operations using pointers – insert
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!