如何编写通用方法来查找最大元素并调用该方法? [英] How to Write a generic method to find the maximal element and invoke that method?

查看:123
本文介绍了如何编写通用方法来查找最大元素并调用该方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从泛型教程中解决练习 Q& A 我的答案略有不同

我的解答

  public static< T extends Comparable< ;? super T>> 
T max(List< ;? extends T> list,int begin,int end)// Option1

public static< T extends Comparable< T>>
T max(List< ;? extends T> list,int begin,int end)//选项2



从下面引用的答案



所以我的问题是


  • Option1:如果 T扩展Object&可比< ;?超级T> 替换为 T扩展Comparable< ;? super T> 。是不是扩展对象隐式?


  • Option2:如果可比< ;? super T> 被替换为 Comparable< T> ?如果是这样的话

  • 扩展Comparable< ;?超级可比<? super T>>>列表; 在Ctrl + 1 max(list,1,10); 这是比特冗长的。如何定义一个扩展 Comparable<的类(层次结构)? super T> ,创建列表并将实例添加到列表并调用下面的方法?基本上我想知道如何在将类实例 A或B 添加到列表中的 max() c $ c> B类扩展A








编写一个通用方法来查找列表范围
[begin,end)中的最大元素。



答案:



  import java.util。*; 

public final class算法{
public static< T extends Object&可比< ;? super T>>
T max(List< ;? extends T> list,int begin,int end){

T maxElem = list.get(begin);

for(++ begin; begin if(maxElem.compareTo(list.get(begin))< 0)
maxElem = list.get(开始);
return maxElem;



$ div $解析方案


如果 Comparable <?>会有什么区别吗? super T> 被替换为 Comparable< T> ?如果是这样的话


请记住 Comparables 总是消费者,即 Comparable< T> 消耗 T 实例,因此应该始终使用 Comparable< ;? super T> 而不是可比< T> (引用 - PECS )。如果您要比较超类实现 Comparable< SuperType> 的类型,则会有所不同。请考虑以下代码:

  class父类实现了Comparable< Parent> {
受保护的字符串名称;

@Override
public int compareTo(Parent o){
return this.name.compareTo(o.name);
}
}

class Child扩展Parent {
public Child(String name){
this.name = name;




$ b现在如果你给你的类型参数设为 T扩展了Comparable< T> ,您将无法像那样将 List< Child> Child 不会执行 Comparable< Child> Comparable< Parent>

  public static< T extends Comparable< T>> T max(List< ;? extends T> list,int begin,int end){
...
}

public static void main(String [] args){
列表< Child> list = new ArrayList< Child>();
max(list,0,2); //当前方法出错。孩子没有实现可比< Child>





$ b

因此,类型参数边界应该是 T extends Comparable< ;?请注意,您不能将您的 Child 类更改为:



  class Child extends Parent implements Comparable< Child> 

因为在这种情况下, Child 类将从相同的不同实例化这是不允许的。







如果<$ code> T扩展了Object&可比< ;?超级T> 替换为 T扩展Comparable< ;? super T> 。是不扩展的对象隐式?


那么,这两个边界之间是有区别的。在1 st 边界中,类型参数的擦除是 Object ,而在第2个nd 边界中,擦除是 Comparable



因此,如果没有 Object bound ,你的代码将被编译为:

  public static Comparable max(List list,int begin,int end)

当您正在生成遗留的非泛型代码时,可能会出现此问题。需要给 Object 作为上限以避免破坏字节码兼容性。您可以通过以下链接阅读更多内容: Angelika Langer - 编程习语

While I was trying to solve exercise from generics tutorial Q&A My answers were slightly different

My Answers

public static <T extends Comparable<? super T>>
    T max(List<? extends T> list, int begin, int end) //Option1

public static <T extends Comparable<T>>
    T max(List<? extends T> list, int begin, int end) //Option2

from quoted answer below

So My question is

  • Option1 :Would it make any difference if T extends Object & Comparable<? super T> is replaced with T extends Comparable<? super T>. Isn't extends Object implicit ?

  • Option2 :Would it make any difference if Comparable<? super T> is replaced with Comparable<T> ? if so How ?

  • Eclipse code completion creates local variable List<? extends Comparable<? super Comparable<? super T>>> list; on Ctrl+1 max(list, 1, 10); which is bit lengthy. How to Define a classes (hierarchy) that extends Comparable<? super T> , create list and add instances to the list and invoke below method ? Basically I want to know how to invoke max() after adding class instances A or B into a list where class B extends A


Write a generic method to find the maximal element in the range [begin, end) of a list.

Answer:

import java.util.*;

public final class Algorithm {
    public static <T extends Object & Comparable<? super T>>
        T max(List<? extends T> list, int begin, int end) {

        T maxElem = list.get(begin);

        for (++begin; begin < end; ++begin)
            if (maxElem.compareTo(list.get(begin)) < 0)
                maxElem = list.get(begin);
        return maxElem;
    }
}

解决方案

Would it make any difference if Comparable<? super T> is replaced with Comparable<T> ? if so How ?

Remember that Comparables are always consumers, i.e., a Comparable<T> consumes T instances, so it should always be preferrable to use Comparable<? super T> instead of Comparable<T> (Quoting - PECS). It would make difference in case you are comparing a type whose super class implements a Comparable<SuperType>. Consider the following code:

class Parent implements Comparable<Parent> {
    protected String name;

    @Override
    public int compareTo(Parent o) {
        return this.name.compareTo(o.name);
    }
}

class Child extends Parent {
    public Child(String name) {
        this.name = name;
    }
}

Now if you give your type parameter as T extends Comparable<T>, you won't be able to call that method for List<Child>, as Child does not implement Comparable<Child> but Comparable<Parent>:

public static <T extends Comparable<T>> T max(List<? extends T> list, int begin, int end) {
    ...
}

public static void main(String[] args) {
    List<Child> list = new ArrayList<Child>();
    max(list, 0, 2);  // Error with current method. Child does not implement Comparable<Child>
}

Hence the type parameter bounds should be T extends Comparable<? super T>.

Note that, you can't change your Child class to:

class Child extends Parent implements Comparable<Child>

because in that case, Child class would extend from different instantiation of same generic type, which is not allowed.


Would it make any difference if T extends Object & Comparable<? super T> is replaced with T extends Comparable<? super T>. Isn't extends Object implicit ?

Well, there is a difference between the two bounds. In the 1st bound, the erasure of the type parameter is Object, whereas in the 2nd bound, the erasure is Comparable.

So, without Object bound, your code will compile to:

public static Comparable max(List list, int begin, int end)

The issue might come when you are generifying the legacy non-generic code. It's neccessary to give Object also as upper bound to avoid breaking the Byte Code compatibility. You can read more about it on this link: Angelika Langer - Programming Idioms

这篇关于如何编写通用方法来查找最大元素并调用该方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆