[英]Java Generic syntax with Comparable interface
I am trying to implement a simple insertion sort algorithm and make it generic to all the instance of Comparable interface. 我正在尝试实现一个简单的插入排序算法,并使它对Comparable接口的所有实例通用。
public static <E extends Comparable<E>> void InsertionSort( E [] array)
{
for(int i = 1; i < array.length; i++)
{
E current = array[i];
int k;
for(k = i-1; k >= 0 && current.compareTo(array[k]) < 0 ; k--)
{
array[k+1] = array[k];
}
array[k+1]=current;
}
for(int l = 0; l < array.length; l++)
{
System.out.print(array[l]+" ");
}
System.out.println();
}
The problem I have is I don't know the differences between 我的问题是我不知道两者之间的区别
<E extends Comparable<E>>
and 和
<E extends Comparable>
They both work but I don't know the reason for the first one. 它们都可以工作,但是我不知道第一个原因。
E extends Comparable<E>
means that an instance of E
can be compared to other objects of type E
. E extends Comparable<E>
指的实例E
可以与类型的其它对象E
。
E extends Comparable
means that an instance of E
can be compared to...something. E extends Comparable
意味着可以将E
的实例与...进行比较。 It's a raw type, which is bad. 这是原始类型,很糟糕。 Don't do that. 不要那样做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.