Question: Write a generic method reverseArray that reverses the order of elements in an array. Use this generic method in a program to reverse arrays of

Write a generic method reverseArray that reverses the order of elements in an array. Use this generic method in a program to reverse arrays of different types.

Print each array both before and after calling reverseArray using a generic method similar to Fig. 20.3.

Fig. 20.3

1 // Fig. 20.3: GenericMethodTest.java 2 // Printing array elements using generic method printArray. 1234 3 4

1 // Fig. 20.3: Generic MethodTest.java 2 // Printing array elements using generic method printArray. 1234 3 4 public class GenericMethodTest { 5 6 7 8 9 10 [I 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 } public static void main(String[] args) { // create arrays of Integer, Double and Character Integer [] integerArray = {1, 2, 3, 4, 5); Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; Character [] characterArray = {'H', 'E', 'L', 'L', '0'}; System.out.printf("Array integerArray contains: "); printArray(integerArray); // pass an Integer array System.out.printf("Array doubleArray contains: "); printArray (doubleArray); // pass a Double array System.out.printf("Array characterArray contains: "); printArray (characterArray); // pass a Character array } // generic method printArray public static void printArray(T[] inputArray) { // display array elements for (T element : inputArray) { System.out.printf("%s ", element); } } System.out.println(); Array integerArray contains : 1 2 3 4 5 Array doubleArray contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array characterArray contains: HELL0

Step by Step Solution

3.45 Rating (155 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To address the question we will create a generic method reverseArray that reverses any type of array ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!