为什么下面的java代码导致编译错误 [英] Why is the following java code leads to compilation error

查看:176
本文介绍了为什么下面的java代码导致编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正致力于让我的代码包含更多泛型。我遇到了一个编译错误,看起来相当复杂,但我可以减少到以下代码中的等效错误:

  List< ;列表与LT;?>> a = new ArrayList< List< Integer>>(); 

为什么会发生这种情况?我可以做些什么来解决这个问题?

解决方案

具有不同类型参数的泛型类的实例不相关,即使 String Object的子类型, List< String> 是不是 List< Object> 的子类型,即使 List< Integer> 是<$ c $的子类型c> List<> List< List< Integer>> 不是的子类型List< List<

$ p $也许你正在寻找

 列表与LT ;?扩展List<>>> a = new ArrayList< List< Integer>>(); 


I am currently working on making my code contain more generics. I encountered a compilation error which looks quite complicated but which I was able to reduce to an equivalent error in the following code:

List<List<?>> a = new ArrayList<List<Integer>>();

Why this happens? What can I do to fix it?

解决方案

Instances of a generic class with different type parameters are not related, i.e. even though String is a subtype of Object, List<String> is not a subtype of List<Object>, and even though List<Integer> is a subtype of List<?>, List<List<Integer>> is not a subtype of List<List<?>>.

Perhaps you are looking for

List<? extends List<?>> a = new ArrayList<List<Integer>>();

这篇关于为什么下面的java代码导致编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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