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
What was the purpose of the Internet Architecture Board?
Nata [24]

Answer:

c to help build the internets infrastrucuter

Explanation:

6 0
2 years ago
Read 2 more answers
Talon is a new game designer working on an exciting and innovative game idea, but its time to examine the idea for technical fea
GarryVolchara [31]

Answer:

I think the answer is C.

Explanation:

4 0
2 years ago
Read 2 more answers
Write a program which will -
Hitman42 [59]

Answer:

down below

Explanation:

score = input() # gets student's score input

max = input() # gets max number

percent = (score/max)*100 # multiply by a hundred to get percentage

if percent > 52: # checks if percent is greater than 52

  print("well dont you have at least a grade 5")

else # if percent is less than or equal to 52 it will print this instead

  print("Unlucky, you need to revise more for the next test.")

THIS PART IS NOT CODE:

make sure you indent/tab the print statements or else you'll get an error

6 0
2 years ago
Does anyone know how to change there Name of the account that shows up? If you do please tell me in a comment and/or Answer.
Alona [7]

Answer:

change your email

Explanation:

award me brainliest

6 0
3 years ago
Read 2 more answers
Which one of the following devices would you choose to meet those requirements?
tensa zangetsu [6.8K]
What are the requirements
5 0
2 years ago
Read 2 more answers
Other questions:
  • What does the Autosum feature on excel do?
    15·1 answer
  • A cell in a spreadsheet can contain _________?
    12·1 answer
  • In a social networking site your personal information is listed under your?
    12·2 answers
  • How can i learn about networks
    15·1 answer
  • What is the output <br>this is a computer science question ​
    8·1 answer
  • What stage of software development incorporates planning to help make changes in the project plan based of reviews
    12·1 answer
  • What function returns a line and moves the file pointer to the next line?
    9·1 answer
  • What is the data and information with example? ​
    9·1 answer
  • Mr. Simmons has assigned a research project. Every student in the class will create a single page report about the recycling hab
    15·1 answer
  • Combination of star topology and star topology can consider as hybrid?​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!