Generic method using an int[] argument
Hello,
I have the following generic function declared.
public static <T extends Comparable<? super T>> void selectionSort (T[] data)
However eclipse will not compile saying:
The method selectionSort(T[]) in the type writeInts is not applicable for the arguments (int[])
Here is the call from main and the rest of the code
Code :
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
boolean found = false;
final int DEFAULT_SIZE = 1000;
int[] rowValues = new int[DEFAULT_SIZE];
writeInts myWriteInts = new writeInts();
found = myWriteInts.selectionSort(rowValues);
;
}
public static <T extends Comparable<? super T>> void selectionSort (T[] data)
{
int min;
T temp;
for (int index = 0; index < data.length-1; index++)
{
min = index;
for (int scan = index+1; scan < data.length; scan++)
if (data[scan].compareTo(data[min])<0)
min = scan;
/** Swap the values */
temp = data[min];
data[min] = data[index];
data[index] = temp;
}
}
Re: Generic method using an int[] argument
int is a primitive data type, and as such Java's generics can't handle them. Instead, try using the Integer wrapper class.
Re: Generic method using an int[] argument