I have a generic class that contains an array. That array can be any data type(Double,Integer,String...) and I am trying to learn how could I sort this array just after constructed. I've wrote a sorting method for sorting the array after constructed. But, java inhibits sorting generic array with mysort method due to mismatch.
public class SortingTest<E> {
private E[] array;
//constructor which creates an object and sorts its array via mysort method.
public SortingTest(E[] N){
this.array = N;
mysort(this.array);
}
//sorts array.
private static<T extends Comparable<T>> void mysort(T[] arr){
T temp = null;
//Bubble sort increasing order.
for(int j=0; j<arr.length; j++){
for(int i=1; i<arr.length; i++){
if(arr[i-1].compareTo(arr[i]) > 0 ){
temp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = temp;
}
}
}
}
}
How can I overcome this problem? Any help is appreciated.
mysort(..)in your constructor. What type is it? Does it match the bounds set by the method.Lists and the rest of the Collections Framework.