Make a String ArrayList.Then write a Java method that swaps the first and last elements.

Answers

Answer 1

Answer:In order to swap elements of ArrayList with Java collections, we need to use the Collections.swap() method. It swaps the elements at the specified positions in the list.

Declaration −The java.util.Collections.swap() method is declared as follows

public static void swap(List <?> list, int i, int j)

where i is the index of the first element to be swapped, j is the index of the other element to be swapped and list is the list where the swapping takes place.

Explanation:

import java.util.*;

public class Example {

  public static void main (String[] args) {

    ArrayList<Integer> list = new ArrayList<Integer>();

     list.add(10);//0

     list.add(20);//1

     list.add(30);//2

     list.add(40);//3

     list.add(50);//4

     System.out.println("Original list : " + list);

     Collections.swap(list, 4, 0); // swapping element at index 3 i.e. 50 and index 1 i.e. 10

     System.out.println("List after swapping : " + list);

  }

}


Related Questions

Other Questions
What is the main benefit of the dsu for team members A 3,204 kg tree positioned on the edge of a cliff 247 m above the ground breaks away and falls into the valley below which is considered zero potential energy. If the trees mechanical energy is conserved, what is the speed of the tree just before it hits the ground in meters/sec? What are some land adaptations of vertebrates? Sales made on account are recorded with a debit to Accounts Receivable and credit to Sales Revenue for the price times the quantity. Management knows that some of those accounts will not be collected but is unsure which specific customers it will be. Thus, management estimates the amount and records an adjusting entry. Later, when the specific non-paying customer is identified, it writes off the account. The effect of the write off on the accounting equation is to _____. Multiple choice question. increase one asset and decrease another increase assets and stockholders' equity increase liabilities and decrease stockholders' equity reduce assets and stockholders' equity the 12-lead ecg shows st-segment elevation in leads v2 and v3 of 0.2 mv. what is this finding indicative of? What sentence has a critical tone? O The show will run from March 20 to April 16 at the Landmark Theater in Ashland. O The script of My Friend Andy imparts the simple wisdom of small-town folks. The intricate costumes and energetic dance numbers bring energy to the show. The amateur cast is unable to convey the significance of the show's message. What sentence has a critical tone ? O The show will run from March 20 to April 16 at the Landmark Theater in Ashland . O The script of My Friend Andy imparts the simple wisdom of small - town folks . The intricate costumes and energetic dance numbers bring energy to the show . The amateur cast is unable to convey the significance of the show's message . the model of an airplane has a wingspan of 20 inches. the model has a scale of 1 inch = 4 feet. what is the wingspan of the actual airplane? show your example imagine a place without a name. mention problems that you are likely to face.why?? in 1979 the Wincom river was 33 feet below the bridge. Because of silt build-up in the river bottom, theriver was only 12 feet below the bridge by 1986. Write an equation for the distance of the river from thebridge, d, with t = 0 representing 1979. If the silt build-up continues at the same rate, what year will theriver reach the bridge? Which value of x would make triangle s u v is-congruent-to triangle t u m by hl? 2 3 4 5 Question 7 of 10 Which system of inequalities is graphed below? 10 -10 -8- 8 10 OA. y > X - 4 y < x +6 OB. VS X - 4 y < x +6 O c. y2 x -4 y > x + E OD. VS X - 4 y > x2 + 6 I need help question 4 A(n) ________ may be used to outline structures of the urinary system, including any stones larger than 2 mm. The transport layer must break messages from the application layer into several _____ that can be sent to the network layer 1. what would be the revised net operating income per month if the sales volume increases by 70 units? 2. what would be the revised net operating income per month if the sales volume decreases by 70 units? 3. what would be the revised net operating income per month if the sales volume is 6,500 units? Judy's Boutique just paid an annual dividend of $2.35 on its common stock. The firm increases its dividend by 3.15 percent annually. What is the company's cost of equity if the current stock price is $38.44 per share As the story opens, Paul is most upset by memories of --Billy Boy's deathhis parentshis training in dealing with couragean old song If you bought a stock last year for a price of $103, and it has risen 13.5% since then, how much is the stock worth now, to the nearest cent? sara scores -4 marks if she got 3 correct answers how many were incorrect What was a criticism of the USA Patriot Act when It was first enacted?A. It did not do enough to protect citizens through the use of government Intelligence gathering.B.it went too far in allowing the government to gather intelligence on private conversations of US citizens.C. It created too many agencies under one umbrella, which would exacerbate rather than amellorate the issues Identified.D.It eliminated the Independent authority of the Federal Bureau of Investigation by bringing it under the act's authority.