为什么这个不能编译:列表<名单,LT;弦乐>> LSS =新的ArrayList< ArrayList的<串GT;>(); [英] Why does this not compile : List<List<String>> lss = new ArrayList<ArrayList<String>>();

查看:136
本文介绍了为什么这个不能编译:列表<名单,LT;弦乐>> LSS =新的ArrayList< ArrayList的<串GT;>();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code:

List<List<String>> lss = new ArrayList<ArrayList<String>>();

这会导致编译时错误:

causes this compile time error :

Type mismatch: cannot convert from ArrayList<ArrayList<String>> to List<List<String>>

解决我改变code为:

to fix I change the code to :

List<ArrayList<String>> lss = new ArrayList<ArrayList<String>>();

为什么被抛出这个错误?这是因为在泛型类型的List 列表&LT;名单&LT;弦乐&GT;&GT; 被实例化,因为列表是一个接口,这是不可能的。

Why is this error being thrown ? Is this because the generic type List in List<List<String>> is instantiated and since List is an interface this is not possible ?

推荐答案

问题是仿制药的类型说明符不(除非你告诉它)允许子类。你必须完全匹配。

The problem is that type specifiers in generics do not (unless you tell it to) allow subclasses. You have to match exactly.

尝试之一:

List<List<String>> lss = new ArrayList<List<String>>();

List<? extends List<String>> lss = new ArrayList<ArrayList<String>>();

这篇关于为什么这个不能编译:列表&LT;名单,LT;弦乐&GT;&GT; LSS =新的ArrayList&LT; ArrayList的&LT;串GT;&GT;();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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