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
77julia77 [94]
2 years ago
14

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

Computers and Technology
1 answer:
Yanka [14]2 years ago
7 0

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);

  }

}

You might be interested in
One purpose of fairy tales is to help children master the problems associated with growing up. Please select the best answer fro
-Dominant- [34]

Answer:

The answer is TRUE

Explanation:

7 0
3 years ago
Read 2 more answers
Assume the integer variable num has been assigned a valid value. What is the purpose of the following code segment?
Pie

Answer:

2

Explanation:

The num%4 operation returns the remainder, so only if num is not a multiple of 4, this value will be non-zero (it will be 1, 2 or 3).

7 0
3 years ago
A(n) _____ is a network connection device that can build tables that identify addresses on each network.
Neko [114]

Answer:

Dynamic Router

Explanation:

5 0
3 years ago
What is the full meaning of ICT
leva [86]
Information commination technology :)
6 0
4 years ago
Read 2 more answers
Why is it a good idea to only use one word for everything you create in Scratch, even though you can technically use two words f
Keith_Richards [23]

Answer:So if your in a situation where you need 1 of those words then your strategy would come in handy.

Explanation:

7 0
2 years ago
Other questions:
  • What kind of device is a printer? output or input
    13·1 answer
  • A phone call costs $0.58 plus $0.15 cents per minute. If the total charge for the call was $4.78, how many minutes was the call?
    15·1 answer
  • In order to install a device, the operating system needs the required __________ for that device.
    15·2 answers
  • Julian is adding a column of numbers as he inputs expenses from his receipts. Holly wants to create a simple comparison between
    13·1 answer
  • Two technicians are discussing bleeding air from the brake hydraulic system. Technician A says to depress the brake pedal slowly
    10·1 answer
  • Write a routine to interchange the mth and nth elements of a singly-linked list. You may assume that the ranks m and n are passe
    10·1 answer
  • 1i) Standardize 'weight' column Write a function named standardize_weight that takes in as input a string and returns an integer
    14·1 answer
  • PLS HELP IMEDIATELY!!!!!
    15·2 answers
  • Which term best describe the operating system of a computer?
    12·1 answer
  • As computer words get larger and larger, there is a law of diminishing returns: the speed of execution of real application progr
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!