
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 3 steps with 1 images

Knowledge Booster
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
- Has to be done in Java. Import ArrayList.arrow_forwardUSING THE FOLLOWING METHOD SWAP CODE: import java.util.*; class ListIndexOutOfBoundsException extends IndexOutOfBoundsException { public ListIndexOutOfBoundsException(String s) { super(s); } } class ListUtil { public static int maxValue(List<Integer> aList) { if(aList == null || aList.size() == 0) { throw new IllegalArgumentException("List cannot be null or empty"); } int max = aList.get(0); for(int i = 1; i < aList.size(); i++) { if(aList.get(i) > max) { max = aList.get(i); } } return max; } public static void swap(List<Integer> aList, int i, int j) throws ListIndexOutOfBoundsException { if(aList == null) { throw new IllegalArgumentException("List cannot be null"); } if(i < 0 || i >= aList.size() || j < 0 || j >= aList.size()) { throw new ListIndexOutOfBoundsException("Index out…arrow_forwardstarter code //Provided imports, feel free to use these if neededimport java.util.Collections;import java.util.ArrayList; /** * TODO: add class header */public class Sorts { /** * this helper finds the appropriate number of buckets you should allocate * based on the range of the values in the input list * @param list the input list to bucket sort * @return number of buckets */ private static int assignNumBuckets(ArrayList<Integer> list) { Integer max = Collections.max(list); Integer min = Collections.min(list); return (int) Math.sqrt(max - min) + 1; } /** * this helper finds the appropriate bucket index that a data should be * placed in * @param data a particular data from the input list if you are using * loop iteration * @param numBuckets number of buckets * @param listMin the smallest element of the input list * @return the index of the bucket for which the particular data should…arrow_forward
- In Java list all the values of the array after the function is called A. static void Fun(int[][]list){ int temp=list[1][2]; list[1][2]=list[0][2]; list[0][2]=temp; } public static void main(String[] args) { int [][]arr={{10,20,30},{100,200,300}}; Fun(arr); } 10,20,30,300 B. static void Fun(int[]list) { list[3]=5;} public static void main(String[] args) { int []arr={5,6,7,9}; Fun(arr); } C. static void Fun(int[][]list){ list[1][0]=0;} public static void main(String[] args) { int [][]arr={{-2,-3,-4},{5,6,7,9}}; Fun(arr); }arrow_forwardjava Create a static method that: is called repeatAll returns ArrayList of Booleans takes in a single parameter - an ArrayList of Booleans This method should modify its ArrayList parameter by repeating its ArrayList values. For example, if the parameter is (true, false, false) The modified ArrayList should be (true, false, false, true, false, false) public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Boolean> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextBoolean()); } System.out.println(repeatAll(list)); } }arrow_forwardThe ArrayList is created using the following syntax: String[] array = {"red", "green", "blue"}; ArrayList list Display all the elements of this list using: a) foreach loop b) foreach method with lambda := new ArrayList(java.util.Arrays.asList(array));arrow_forward
- This is using Data Structures in Javaarrow_forwardAre they equivalent? 1) ArrayList<?> list = new ArrayList(); 2) ArrayList<? extends Object> list = new ArrayList<>();arrow_forwardGiven an ArrayList called myAL that contains the following: ["blue", "red", "yellow"] write code to insert the elements "purple" and "orange" that keeps the list in alphabetical order.arrow_forward
- Given the following class declaration: class List { public: List(); List(const List&); List& operator=(const List&); -List(); // Inserts element in the specified position, returns true if // it was able to insert. // Example, if the list is [4, 2, 1], and the user // calls list.Insert(6, 1) the list would become [4, 6, 2, 1] bool Insert(double element, size_t position) ; // Returns the position of element if found, if not found returns -1 int Index0f(double element)const ; // Removes the element in the given position double Remove(size_t position) ; // Gets the number in the given position double Get(size_t position)const ; // Returns a string representation of the list, elements // separated by commas and the list between brackets string ToString()const ; // Returns the number of elements in the list size_t Size)const; // Returns true if the list is empty, false otherwise bool IsEmpty)const; // Clears the whole list void Clear(); private: struct Node{ double number; Node* next; };…arrow_forwardstarter code import java.util.ArrayList; public class SortsTracing { //Note: Style is not required for this file //for reference public ArrayList<int[]> SelectionSortExampleList() { ArrayList<int[]> answer = new ArrayList<int[]>(); answer.add(new int[]{-1, 20, 18, 17, 9, 4, 2, 0, 40}); answer.add(new int[]{-1, 0, 18, 17, 9, 4, 2, 20, 40}); answer.add(new int[]{-1, 0, 2, 17, 9, 4, 18, 20, 40}); answer.add(new int[]{-1, 0, 2, 4, 9, 17, 18, 20, 40}); answer.add(new int[]{-1, 0, 2, 4, 9, 17, 18, 20, 40}); answer.add(new int[]{-1, 0, 2, 4, 9, 17, 18, 20, 40}); // etc... (the rest of the iterations) return answer; } public ArrayList<int[]> InsertionSortRandomList() { ArrayList<int[]> answer = new ArrayList<int[]>(); answer.add(new int[]{30, 62, 5, 109, 66, 17, 51, 18}); // TODO return answer; } public ArrayList<int[]>…arrow_forwardTo declare an array of String called myStrings, we would code: ArrayList<Blank 1> myStrings = Blank 2 ArrayList<String>();arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education