使用ArrayList<E>上的比较器

huangapple go评论83阅读模式
英文:

Using a comparator on ArrayList<E>

问题

我一直在尝试对一个以下显示的自定义比较器的 ArrayList<E> 进行排序:

@Override
public int compare(Dog d1, Dog d2){
    return Integer.compare(d1.tired, d2.tired);
};

现在,在我继续之前,我只想让这个工作起来:

public static <E> ArrayList<E> listBiggest(ArrayList<E> list) {
    Dog dogcomparator = new Dog();
    Collections.sort(list, dogcomparator);
}

尽管我无法对列表进行排序,因为显然列表没有符合比较器类型的对象,尽管列表全部都是该类型或扩展该类型的对象。

我不确定我做错了什么。

我得到的错误是:

错误:(15, 20) java: 没有找到合适的方法来对 java.util.ArrayList<E> 进行排序
方法 java.util.Collections.sort(java.util.List<T>) 不适用
(无法推断出类型变量 T
   (实际和形式参数列表长度不同))
方法 java.util.Collections.sort(java.util.List<T>,java.util.Comparator<? super T>) 不适用
(推断变量 T 具有不兼容的边界
   相等约束:E
   下限:Dog,java.lang.Object)

我是否忽略了一些我不知道的基本内容?甚至是否有足够的信息来理解我所尝试提问的要点?

英文:

I've been trying to sort an ArrayList&lt;E&gt; with a custom comparator shown below

@Override
public int compare(Dog d1, Dog d2){
    return Integer.compare(d1.tired, d2.tired);
};

Now this is all I want to get working before I move on:

public static &lt;E&gt; ArrayList&lt;E&gt; listBiggest(ArrayList&lt;E&gt; list) {
    Dog dogcomparator = new Dog();
    Collections.sort(list, dogcomparator);
}

Although I can't sort the list because it's of apparently list has no objects which conform to the comparator type although the list is all of that type or objects which extend it.

I'm not sure what I'm doing wrong.

The error I am getting is

Error:(15, 20) java: no suitable method found for sort(java.util.ArrayList&lt;E&gt;,Dog)
method java.util.Collections.&lt;T&gt;sort(java.util.List&lt;T&gt;) is not applicable
  (cannot infer type-variable(s) T
    (actual and formal argument lists differ in length))
method java.util.Collections.&lt;T&gt;sort(java.util.List&lt;T&gt;,java.util.Comparator&lt;? super T&gt;) is not applicable
  (inference variable T has incompatible bounds
    equality constraints: E
    lower bounds: Dog,java.lang.Object)

Am I missing something fundamental that I don't know about? Is there even enough information to get the gist of what I am trying to ask?

答案1

得分: 1

你有一个 List<E>,你想使用一个 Comparator<Dog> 对其进行排序。编译器不知道什么是 E;对于它来说,E 可能是一个 Person。如果你确定它是一个狗的列表,进行类型转换。

英文:

You’ve a List&lt;E&gt; which you want to sort using a Comparator&lt;Dog&gt;. The compiler doesn’t know what’s E; for all it knows, E could be a Person.
If you’re sure it’s a list of dogs, cast it.

答案2

得分: 0

改变该方法的签名。如果其中的所有内容都需要是某种类型的 Dog,那就声明出来。

public static <E extends Dog> List<E> listBiggest(List<E> list) {

最好以与 Collections.sort 相同的方式声明 ListArrayList 实现的接口)。这将使该方法适用于任何类型的列表,而不仅仅是特定的实现。

你还需要在方法中添加一个 return,或者将方法的返回类型设为 void

英文:

Change the signature of the method. If everything in there is required to be some kind of Dog then declare that.

public static &lt;E extends Dog&gt; List&lt;E&gt; listBiggest(List&lt;E&gt; list) {

It's better to declare List (the interface that ArrayList implements) the same way that Collections.sort does, as well. This will let the method be used for any sort of list, not just that one specific implementation.

You'll also need to add a return to the method, or make the method return type void.

huangapple
  • 本文由 发表于 2020年10月23日 07:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64492086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定