java In this assignment you will swap a position in an array list with another. swap() gets 3 arguments, an Arraylist, a position, and another position to swap with. Example   swap(["one","two","three"],0,2)   returns:["three","two","one"]   public static ArrayList swap(ArrayList list,int pos1,int pos2)    public static void main(String[] args) {     Scanner in = new Scanner(System.in);     int size = in.nextInt();     int pos1 = in.nextInt();     int pos2 = in.nextInt();     ArrayList list = new ArrayList<>();     for(int i=0; i < size; i++) {       list.add(in.next());     }     System.out.println(swap(list, pos1, pos2));   }  }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question

java

In this assignment you will swap a position in an array list with another.

swap() gets 3 arguments, an Arraylist, a position, and another position to swap with.

Example

 
swap(["one","two","three"],0,2)
 
returns:["three","two","one"]
 
public static ArrayList<String> swap(ArrayList<String> list,int pos1,int pos2) 
 

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    int pos1 = in.nextInt();
    int pos2 = in.nextInt();
    ArrayList<String> list = new ArrayList<>();
    for(int i=0; i < size; i++) {
      list.add(in.next());
    }

    System.out.println(swap(list, pos1, pos2));

  } 

}

 
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT