'Why is there an error in generics when sorting an array?
You need to write an array sort. But there is an error "Operator '>' cannot be applied to 'T', 'T'" and "Incompatible types. Found: 'T[]', required: 'T'". What is the problem, why is it?
public class MySortArray <T extends Comparable <T>>{
public T SortArray(T[] array) {
int temp;
for(int i = 0; i < array.length - 1; i++) {
if(array[i] > array[i+1]) {
}
}
return array;
}
}
Solution 1:[1]
Java does not support operator overloading.
You must use the compareTo() method of Comparable:
if (array[i].compareTo(array[i+1]) > 0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bohemian |
