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
In microsoft word, when you highlight existing text you want to replace, you’re in
Sergio039 [100]
 Hello <span>Christinamauffp2olwu</span><span>

Answer: In Microsoft Word, when you highlight existing text you want to replace, you're in insert mode.

Hope This Helps :-)
-Chris</span>
6 0
3 years ago
What does a professional programmer usually do first to gain an understanding of a problem?
ElenaW [278]
The correct answer to your question is:

Professional programmers work directly with, and interview customers to find out what the problem is.
6 0
2 years ago
Read 2 more answers
Match the characteristics to the mobile operating system that it describes.
mel-nik [20]

Answer:

1. is Apple's iOS

2. is Android

3. is Microsoft

8 0
3 years ago
What would be the most popular piece of technology on Earth?
spin [16.1K]

Answer:

the smart phone

Explanation:

It is the most widley used and accepted

8 0
3 years ago
Read 2 more answers
Write the name of the tab, the command group, and the icon that you need to use to justify
Dafna11 [192]

Answer:

Tab: Home Tab

Command Group: Paragraph

Icon: Justify Icon

<em>The icon has no name; rather it is represented by 4 horizontal lines that are aligned on both edges</em>

<em />

Explanation:

To answer this question, I'll make use of the following attachment

<em>Where</em>

<em>1- represents the home tab</em>

<em>2 - represents the paragraph</em>

<em>3 - represents the justify icon</em>

<em />

3 0
3 years ago
Other questions:
  • Suppose that a computer virus infects your computer and corrupts the files you were going to submit for your current homework as
    12·1 answer
  • Write a method so that the main() code below can be replaced by the simpler code that calls method mphandminutestomiles(). origi
    14·2 answers
  • What does the presence of the cydia app on an ios device indicate?
    14·1 answer
  • Rate is how fast a screen updates the information being displayed.
    6·2 answers
  • Write a grammar for the language consisting of strings built only of the letters a and b. The strings may have any number of the
    6·1 answer
  • A method that receives a two-dimensional array uses two ____ pairs following the data type in the parameter list of the method h
    11·1 answer
  • How would you convert an integer value to a float value in Python?
    10·1 answer
  • ​Client/server computing is​ a: A. network that connects sensors to desktop computers. B. distributed computing model where clie
    12·1 answer
  • Stephanie would like to know the average number of regular hours worked by her employees. In cell B11, create a formula using th
    8·1 answer
  • How many pages is 1500 words double spaced 12 font?.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!