public class QS {
    public static <T extends Comparable<? super T>> T[] quickSort(T[]array) {
        return doSort(array, 0, array.length - 1);
    //some other methods.................
    public static void main(String [] args){
        QS qs = new QS();
        int [] nums = {4,7,23,1,2,45,23,11};
        qs.quickSort(nums);//gives error

I am getting an error in which it won't allow me to do a quicksort on an int array.

the error message is <T>quickSort(T[]) in QS cannot be applied to (int[])

update: if i changed my int [] to a String [], then it works no problem..

Edited by plasticfood

4 Years
Discussion Span
Last Post by plasticfood

Your Generic class definition says that the type T of the array must extend/implement Comparable. int isn't an Object, and doesn't implement anything, so your class cannot work with an int array.
In fact all generic types must be Objects, never primitives, so there's nothing you can do to make your generic class work with ints.
If you change to an array of Integers that would work, Integer implements Comparable.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.