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..

Recommended Answers

All 2 Replies

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.

Thanks got it now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.