Find out implementation type of input interface

53 views Asked by At

I have a function which has the interface Comparable as input parameter.

int sort(Comparable A[]){...}

Now I want to make a temporary variable of that type of which A is at run time (might be Integer or Float, but I don't know), to do comparisons for my sorting algorithm.
How is it done? I'm a freshman to Java.

1

There are 1 answers

3
Elliott Frisch On BEST ANSWER

First, please don't use Raw Types. Second, I don't think you can but you could make a temporary variable of Comparable. Something like,

static <T> void sort(Comparable<T> A[]){
    if (A == null || A.length == 0) {
        return;
    }
    Comparable<T> temp = A[0];
    // ...
}