Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a generic method to exchange the positions of two different elements in an array. Lets say the method name is swap. It will take

image text in transcribed
Write a generic method to exchange the positions of two different elements in an array. Lets say the method name is "swap". It will take three parameters, a generic array called 'a', and two integer positions (i and j). For method swaps the element of 'a' i^th element of array 'a' with the j^th element. For example, I have provided some test calls of the swap method from the main that shows how this method could be called for two different types of array. Your program should work without changing anything in the main method. public class GenericAlgorithm {//define the public static void swap (generic) method here public static void main (String [] args) {Integer [] List = {1, 5, 4, 10};//call swap on an Integer array swap (iList, 1, 2); for (Integer i: iList) System.out.print (i +", ");//should print 1, 4, 5, 10, String [] sList = {"one", "five", "four", "ten"};//call swap on a string array swap (sList, 3, 2); for (String s: sList) System.out.print (s + ", ");//should print one, five, ten, four, }}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

What are some attractive features of a twin-screw extruder design?

Answered: 1 week ago

Question

Determine the roles of spatial layout and functionality.

Answered: 1 week ago

Question

Be familiar with the basic ways to manage capacity.

Answered: 1 week ago