Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Homework. I'm help understanding why my code will compile but leaves notes in the compiler about array problems?

Tags:

java

Note: ArrayOperation.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

public class ArrayOperation{
public static void sort(Comparable[] c){
    for (int i=1;i<c.length;i++){
    Comparable key = c[i];
    int p = i;
    while (p>0 && key.compareTo(c[p-1])<0){
        c[p]=c[p-1];
        p--;
    }
    c[p] = key;
    }
}

}

like image 776
Divan Jekels Avatar asked Dec 09 '12 21:12

Divan Jekels


1 Answers

you have to provide type parameter for java.lang.comparable, If you check the API for java.lang.Comparable, it expects a type parameter.

Interface Comparable<T>

thus, your method signature should be if you want compile time warnings to disappear.

public static void sort(Comparable<SomeType>[] c){
like image 96
PermGenError Avatar answered Oct 09 '22 17:10

PermGenError