I have tried the approach below,but still keep getting the arrayindexoutofbound exception.
public class Test {
private Object[] o;
String type, s[];
Integer[] i = new Integer[50];
Object temp = null;
public <T> Test(T[] obs) {
this.o = obs;
if (obs[1] instanceof String) {
type = "string";
} else if (obs[1] instanceof Integer) {
type = "integer";
}
if (type == "string") s = (String[]) o;
else if (type == "integer") i = (Integer[]) o;
// System.out.println(type);
}
public int compare(int one, int two) {
if (type == "string") {
int l = s[one].compareTo(s[two]);
if (l == 0) return 0;
else if (l > 0) return 1;
else return -1;
} else if (type == "integer") {
System.out.println("integer");
for (int a : i) System.out.println(a);
if (i[one] > i[two]) return 1;
else if (i[one] < i[two]) return -1;
else return 0;
}
return 4;
}
public void swap(int i, int j) {
o[i] = temp;
o[i] = o[j];
o[j] = temp;
}
}
The class where i am implementing the above compare for bubblesort:-
import jsoup.Test;
public class BubbleInt {
public static void main(String args[]) {
Integer[] arr = {2, 1, 8, 7};
Test t = new Test(arr);
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 1; j < arr.length - i; j++) {
int val = t.compare(arr[j - 1], arr[j]);
System.out.println(val);
if (val > 1) t.swap(arr[j - 1], arr[j]);
}
}
for (int i : arr) {
System.out.println(i);
}
}
}