Java嵌套的泛型类型不匹配 [英] Java nested generic type mismatch

查看:317
本文介绍了Java嵌套的泛型类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中:

  public static void main(String [] args){

列表< String> b = new ArrayList< String>();
先(b); $ b b秒(b);

列表< List< String>> a = new ArrayList< List< String>>();
第三(a);
第四(a); //不工作

}

私人静态< T> void(first< T> a){
System.out.println(List of T);


private static void second(List<?> a){
System.out.println(List of anything);
}

私人静态< T> void third(List< List< T>> a){
System.out.println(List of T of List);


private static void fourth(List< List<>> a){
System.out.println(任何列表的列表);





为什么第二个(b)的调用有效,但是致电第四(a)不?



我收到以下错误:

类型TestTest中的方法fourth(List< List<>)不适用于参数(`List< List< String>>`)


解决方案

如果您希望能够调用第四与一个 List< List< String>> 参数,那么您需要将签名更改为:

  private static void fourth(List< ;? extends List<>> a){
System.out.println(List任何事物的清单);
}

因为不像 List< List< ?>> 列表< ;? extends List<>> List< List< String>> 兼容。以这种方式考虑:

  List< List< String>> original = null; 
列表< ;?扩展List<>>>好的=原创; //这工作
列表<?> ok2 =原创; //这个
List< List<>>> notOk = original; //这不是

List< Integer> original = null;
列表< ;?扩展Number>好的=原创; //这工作
列表<?> ok2 =原创; //这个
列表< Number> notOk = original; //这不是

推理很简单。如果您有

  private static void fourth(List< List<>> a){
List< ?> ohOh = Arrays.asList(new Object());
a.add(ohOh);
}

然后,如果可以将该方法称为例如:

  List< List< String>> a = new ArrayList< List< String>>(); 
第四(a);
String fail = a.get(0).get(0); // ClassCastException here!


In the following example:

public static void main(String[] args) {

    List<String> b = new ArrayList<String>();
    first(b);
    second(b);

    List<List<String>> a = new ArrayList<List<String>>();
    third(a);
    fourth(a);  // doesnt work

}

private static <T> void first(List<T> a){
    System.out.println("List of T");
}

private static void second(List<?> a){
    System.out.println("List of anything ");
}

private static <T> void third(List<List<T>> a){
    System.out.println("List of a List of T ");
}

private static void fourth(List<List<?>> a){
    System.out.println("List of a List of anything ");
}

Why does the call to second(b) work, but the call to fourth(a) doesn't ?

I get the following error:

The method fourth(List<List<?>>) in the type `TestTest` is not applicable for the arguments (`List<List<String>>`)

解决方案

If you want to be able to call fourth with a List<List<String>> argument, then you'll need to change your signature to this:

private static void fourth(List<? extends List<?>> a){
    System.out.println("List of a List of anything ");
}

The above will work because unlike List<List<?>>, List<? extends List<?>> is compatible with List<List<String>>. Think of it this way:

List<List<String>> original = null;
List<? extends List<?>> ok  = original; // This works
List<?> ok2                 = original; // So does this
List<List<?>> notOk         = original; // This doesn't

List<Integer> original      = null;
List<? extends Number> ok   = original; // This works
List<?> ok2                 = original; // So does this
List<Number> notOk          = original; // This doesn't

The reasoning is simple. If you had

private static void fourth(List<List<?>> a) {
    List<?> ohOh = Arrays.asList(new Object());
    a.add(ohOh);
}

And then if you could call that method as such:

List<List<String>> a = new ArrayList<List<String>>();
fourth(a);
String fail = a.get(0).get(0); // ClassCastException here!

这篇关于Java嵌套的泛型类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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