I have the following code:
public static <T extends Comparable<T>> T[] getRandomPermutationOfIntegers(int size) {
T[] data = (T[])new Comparable[size];
for (int i = 0; i < size; i++) {
data[i] = i;
}
// shuffle the array
for (int i = 0; i < size; i++) {
int temp;
int swap = i + (int) ((size - i) * Math.random());
temp = data[i];
data[i] = data[swap];
data[swap] = temp;
}
return data;
}
which permutes an array of integers and return them. I want to fill the array with int values but am getting error in the two for() loops since T is different from int.
How do i fix them to make them work?
Use the Integer wrapper instead of the primitive int.