什么类型为<>制作实例列表时? [英] What type is <?> when making instantiating lists?

查看:137
本文介绍了什么类型为<>制作实例列表时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了多个不同地方的人谁实例化一个列表或者类似的ArrayList

 列表<> L =新的ArrayList<>();

是什么类型?这是否意味着它可以在它持有任何类型的?如果是这样,为什么会变成这样,而不是使用的只是和ArrayList <?/ p>

解决方案

  

这是否意味着它可以在它持有任何类型的?


没有。这意味着,你的变量可以指任何类型的参数列表。因此,它实际上是一个的限制的:你会不会被允许任何对象添加到,因为你不知道它接受哪些项目。举一个具体的例子,可以是一个列表&LT;弦乐&GT; 或者它可能是一个列表&LT;&ExecutorService的GT;

I have seen in multiple different places people who instantiate a list or ArrayList like:

List<?> l = new ArrayList<>();

What type is ?? Does this mean that it can hold any types in it? If so, why would this be used instead of just and ArrayList?

解决方案

Does this mean that it can hold any types in it?

No. It means that your l variable could be referring to a list parameterized with any type. So it's actually a restriction: you will not be allowed to add any object to l because you have no idea which items it accepts. To give a concrete example, l could be a List<String> or it could be a List<ExecutorService>.

这篇关于什么类型为&lt;&GT;制作实例列表时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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