This is the second solution to reverse all the data values in an Array. This solution uses Java Generics, which defines a Generic method for reversing given Array data values. This solution modifies the same array, and the resultant array has all the data values reversed. The data values are of any type such as Integers, Floats, Doubles, Characters, and Strings.
public class ReverseArrayValuesGeneric {
static int indexVal=0;
static String when[] = {"Before","After"};
//Generic method to process the reverse logic in an Array of values
private static <E> void reverseArrayData(E[] numArray) {
// Calculate the rear Index position in an array based on the length
int startIdx = 0, rearIdx = numArray.length - 1;
/*
* Start swapping the data at first and last positions of an array, do continue
* swapping data by increasing first index to the next higher index, and by
* decreasing last index to next lower index, until those positions cross each
* or have same value
*/
while (startIdx <= rearIdx) {
E tempNum = numArray[startIdx];
numArray[startIdx] = numArray[rearIdx];
numArray[rearIdx] = tempNum;
startIdx += 1;
rearIdx -= 1;
}
}
// Generic method to display an array values in some specific format
private static <E> void dispArray(E[] numArray) {
String dataType=numArray.getClass().getSimpleName().replace("[]", "s");
System.out.println("An Array of "+dataType+" - "+when[indexVal]+" reversing");
System.out.print("[ ");
for (int i = 0; i < numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
System.out.println("]");
indexVal ^=1;
}
public static void main(String[] args) {
// Initialized an Array to Integer values
Integer[] numArray = { 21, 98, 42, 59, 79, 65, 32 };
dispArray(numArray);
reverseArrayData(numArray);
dispArray(numArray);
System.out.println();
// Initialized an Array to Float values
Float[] floatArray = { 12.0f, 89.0f, 24.0f, 95.0f, 97.0f, 56.0f, 23.0f };
dispArray(floatArray);
reverseArrayData(floatArray);
dispArray(floatArray);
System.out.println();
// Initialized an Array to Double values
Double[] doubleArray = { 21.0, 98.0, 42.0, 59.0, 79.0, 65.0, 32.0 };
dispArray(doubleArray);
reverseArrayData(doubleArray);
dispArray(doubleArray);
System.out.println();
// Initialized an Array to Characters
Character[] charArray = { 'G', 'I', 'L', 'K', 'N', 'A' };
dispArray(charArray);
reverseArrayData(charArray);
dispArray(charArray);
System.out.println();
// Initialized an array to Strings
String[] strArray = { "JOHN", "DAVE", "RUSS", "MIKE", "SANJAY" };
dispArray(strArray);
reverseArrayData(strArray);
dispArray(strArray);
}
}
Output:
An Array of Integers - Before reversing
[ 21 98 42 59 79 65 32 ]
An Array of Integers - After reversing
[ 32 65 79 59 42 98 21 ]
An Array of Floats - Before reversing
[ 12.0 89.0 24.0 95.0 97.0 56.0 23.0 ]
An Array of Floats - After reversing
[ 23.0 56.0 97.0 95.0 24.0 89.0 12.0 ]
An Array of Doubles - Before reversing
[ 21.0 98.0 42.0 59.0 79.0 65.0 32.0 ]
An Array of Doubles - After reversing
[ 32.0 65.0 79.0 59.0 42.0 98.0 21.0 ]
An Array of Characters - Before reversing
[ G I L K N A ]
An Array of Characters - After reversing
[ A N K L I G ]
An Array of Strings - Before reversing
[ JOHN DAVE RUSS MIKE SANJAY ]
An Array of Strings - After reversing
[ SANJAY MIKE RUSS DAVE JOHN ]

Leave a comment