Consider the code:
考虑一下代码:
public abstract class Item<T> implements Comparable<T>
{
protected T item;
public int compareTo(T o)
{
return 0; // this doesn't matter for the time being
}
}
public class MyItem<T> extends Item<String>
{
T object;
}
public class Foo<T>
{
protected ArrayList<T> list;
}
public class Bar<V> extends Foo<MyItem<V>>
{
public void sort()
{
Collections.sort(list);
}
}
The sort call gives the error:
排序调用给出错误:
Bound mismatch: The generic method sort(List< T >) of type Collections is not applicable for the arguments (ArrayList< MyItem< T > >). The inferred type MyItem< T > is not a valid substitute for the bounded parameter < T extends Comparable< ? super T > >
绑定不匹配:类型集合的泛型方法排序(List< t>)不适用于参数(ArrayList< MyItem< T > >)。推断式MyItem< T >不是一个有效的替代品,对于有界参数< T可以进行比较< ?超级T > >
Why is this wrong?
为什么这是错的吗?
If MyItem<V>
implements Comparable
then why is it not a substitute?
如果MyItem
Sorry if this has been asked, but I feel the question is somewhat specific.
对不起,如果有人问这个问题,我觉得这个问题有点具体。
4 个解决方案
#1
11
Actually more detailed explanation of this error gives your javac
itself:
实际上,对这个错误的更详细的解释给了javac本身:
java: no suitable method found for
sort(java.util.ArrayList<MyItem<V>>
)java:没有找到适合排序的方法(java.util. arraylist
>) method
java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>)
is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length)方法java.util.Collections。< T >排序(并不知道< T >,java.util.Comparator < ?“超级T>”是不适用的(不能从参数中实例化,因为实际和正式的参数列表的长度不同)
method
java.util.Collections.<T>sort(java.util.List<T>)
is not applicable (inferred type does not conform to declared bound(s) inferred:MyItem<V>
bound(s):java.lang.Comparable<? super MyItem<V>>
)方法java.util. collection .
sort(java.util.List )是不适用的(推断类型不符合声明的绑定(s): MyItem 绑定(s): java.lang.Comparable >)
So, the main question is:
why is method Collections.<T>sort(java.util.List<T>)
) not applicable?
所以,主要的问题是:为什么方法集合?
The answer is:
because in Collections.<T>sort(java.util.List<T>)
method declaration there are bounds on parameter T
: <T extends Comparable<? super T>>
.
答案是:因为在集合中。
In another words, T
must implement Comparable
interface on it self. For example String
class implements such interface: ...implements ... Comparable<String>
.
换句话说,T必须在它自身上实现类似的接口。例如,String类实现了这样的接口:…实现了……类似 <字符串> 。
In your case Item
class doesn't implement such interface:
在您的case Item类中没有实现这样的接口:
Item<T> implements Comparable<T>
is not same thing as Item<T> implements Comparable<Item<T>>
.
项目
So, for solving this problem, your should change your Item
class to this one:
因此,为了解决这个问题,您应该将您的项目类改为这个:
public abstract class Item<T> implements Comparable<Item<T>>
{
protected T item;
public int compareTo(Item<T> o)
{
return 0; // this doesn't matter for the time being
}
}
#2
2
For objects of type X
to be comparable with each other, class X
has to implement exactly Comparable<X>
.
对于X类型的对象可以相互比较,类X必须实现完全可比的
This is not what your code is doing, you've got a class Item<T>
and you are implementing Comparable<T>
instead of Comparable<Item<T>>
. This means that Item<T>
can be compared with T
, but not with Item<T>
, which is required.
这不是您的代码正在做的事情,您有一个类项
Change your Item<T>
class to:
将您的项目
public abstract class Item<T> implements Comparable<Item<T>>
{
protected T item;
@Override
public int compareTo(Item<T> o)
{
return 0; // this doesn't matter for the time being
}
}
#3
1
Just change the class like follow:
只需要更改类如下:
public class MyItem<T> extends Item<String> implement Comparable<MyItem<T>>
{
T object;
}
Or
或
public abstract class Item<T> implements Comparable<MyItem<T>>
{
protected T item;
public int compareTo(MyItem<T> o)
{
return 0; // this doesn't matter for the time being
}
}
}
The error tips has shown us.Hope it helpful.
错误提示已经向我们展示了。希望它对您有所帮助。
#4
0
You do not need to have the class MyItem
generified just to see the effect. The following class is enough to see what happens:
您不需要将类MyItem泛化就可以看到效果。下面的课就足以让我们看看会发生什么:
public class MyItem extends Item<String> {}
Now you have the following call:
现在你有了以下的电话:
Collections.sort(list);
As morgano stated correctly, the sort method will take a collection that is parameterized with a type T that must be comparable to T. Your MyItem
class is extending Item<String>
, which results in MyItem
being comparable to String
s.
正如morgano所指出的,sort方法将接受一个参数化的集合,该集合必须与T类型相比较。您的MyItem类是扩展项
With a little switch in which class implements the Comparable
interface, you will get the expected result:
通过一个小开关,类实现了可比较的接口,您将得到预期的结果:
public abstract class Item<T> {
protected T item;
}
public class MyItem extends Item<String> implements Comparable<MyItem> {
@Override
public int compareTo(MyItem o) {
return item.compareTo(o.item); // just an example
}
}
And now the call to Collections.sort(list)
will work.
现在,集合排序的调用将会起作用。
#1
11
Actually more detailed explanation of this error gives your javac
itself:
实际上,对这个错误的更详细的解释给了javac本身:
java: no suitable method found for
sort(java.util.ArrayList<MyItem<V>>
)java:没有找到适合排序的方法(java.util. arraylist
>) method
java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>)
is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length)方法java.util.Collections。< T >排序(并不知道< T >,java.util.Comparator < ?“超级T>”是不适用的(不能从参数中实例化,因为实际和正式的参数列表的长度不同)
method
java.util.Collections.<T>sort(java.util.List<T>)
is not applicable (inferred type does not conform to declared bound(s) inferred:MyItem<V>
bound(s):java.lang.Comparable<? super MyItem<V>>
)方法java.util. collection .
sort(java.util.List )是不适用的(推断类型不符合声明的绑定(s): MyItem 绑定(s): java.lang.Comparable >)
So, the main question is:
why is method Collections.<T>sort(java.util.List<T>)
) not applicable?
所以,主要的问题是:为什么方法集合?
The answer is:
because in Collections.<T>sort(java.util.List<T>)
method declaration there are bounds on parameter T
: <T extends Comparable<? super T>>
.
答案是:因为在集合中。
In another words, T
must implement Comparable
interface on it self. For example String
class implements such interface: ...implements ... Comparable<String>
.
换句话说,T必须在它自身上实现类似的接口。例如,String类实现了这样的接口:…实现了……类似 <字符串> 。
In your case Item
class doesn't implement such interface:
在您的case Item类中没有实现这样的接口:
Item<T> implements Comparable<T>
is not same thing as Item<T> implements Comparable<Item<T>>
.
项目
So, for solving this problem, your should change your Item
class to this one:
因此,为了解决这个问题,您应该将您的项目类改为这个:
public abstract class Item<T> implements Comparable<Item<T>>
{
protected T item;
public int compareTo(Item<T> o)
{
return 0; // this doesn't matter for the time being
}
}
#2
2
For objects of type X
to be comparable with each other, class X
has to implement exactly Comparable<X>
.
对于X类型的对象可以相互比较,类X必须实现完全可比的
This is not what your code is doing, you've got a class Item<T>
and you are implementing Comparable<T>
instead of Comparable<Item<T>>
. This means that Item<T>
can be compared with T
, but not with Item<T>
, which is required.
这不是您的代码正在做的事情,您有一个类项
Change your Item<T>
class to:
将您的项目
public abstract class Item<T> implements Comparable<Item<T>>
{
protected T item;
@Override
public int compareTo(Item<T> o)
{
return 0; // this doesn't matter for the time being
}
}
#3
1
Just change the class like follow:
只需要更改类如下:
public class MyItem<T> extends Item<String> implement Comparable<MyItem<T>>
{
T object;
}
Or
或
public abstract class Item<T> implements Comparable<MyItem<T>>
{
protected T item;
public int compareTo(MyItem<T> o)
{
return 0; // this doesn't matter for the time being
}
}
}
The error tips has shown us.Hope it helpful.
错误提示已经向我们展示了。希望它对您有所帮助。
#4
0
You do not need to have the class MyItem
generified just to see the effect. The following class is enough to see what happens:
您不需要将类MyItem泛化就可以看到效果。下面的课就足以让我们看看会发生什么:
public class MyItem extends Item<String> {}
Now you have the following call:
现在你有了以下的电话:
Collections.sort(list);
As morgano stated correctly, the sort method will take a collection that is parameterized with a type T that must be comparable to T. Your MyItem
class is extending Item<String>
, which results in MyItem
being comparable to String
s.
正如morgano所指出的,sort方法将接受一个参数化的集合,该集合必须与T类型相比较。您的MyItem类是扩展项
With a little switch in which class implements the Comparable
interface, you will get the expected result:
通过一个小开关,类实现了可比较的接口,您将得到预期的结果:
public abstract class Item<T> {
protected T item;
}
public class MyItem extends Item<String> implements Comparable<MyItem> {
@Override
public int compareTo(MyItem o) {
return item.compareTo(o.item); // just an example
}
}
And now the call to Collections.sort(list)
will work.
现在,集合排序的调用将会起作用。