Write a Java program that defines a generic method called swap within a class named GenericMethodExample.
The swap method should accept an array of any data type and two indices within the array. It should swap
the values at the specified indices in the array. If the indices are out of bounds, the method should throw an
ArrayIndexOutOfBoundsException (do not rely on the built-in one to do it for you). When finished, upload
your GenericMethodExample.java file to the dropbox.
Tester
Use the following Java code to test your solution:
public class GenericMethodExample {
//A generic method that swaps the values of two elements
//in an array is implemented here.
public static void main(String[] args) {
Integer [] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"one", "two", "three", "four"};
System.out.println("Before swapping (integer array):");
for (Integer num : intArray) {
System.out.print(num + " ");
}
System.out.println();
swap(intArray, 1, 3); // swap elements at index 1 and 3
System.out.println("After swapping (integer array):");
for (Integer num : intArray) {
System.out.print(num + " ");
}
System.out.println();
System.out.println("Before swapping (string array): ");
for (String str : strArray) {
System.out.print(str + " ");
}
System.out.println();
swap(strArray, 0, 2); // swap elements at index 0 and 2
System.out.println("After swapping (string array):");
for (String str : strArray) {
System.out.print(str + " ");
}
System.out.println();
}
}