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

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

问题描述

我在多个不同的地方看到过实例化列表或 ArrayList 的人,例如:

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

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

是什么类型??这是否意味着它可以容纳任何类型?如果是这样,为什么要使用它而不是仅使用 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?

没有.这意味着您的 l 变量可以引用任何类型参数化的列表.所以它实际上是一个限制:你不能向 l 添加任何对象,因为你不知道它接受哪些项目.举一个具体的例子,l 可以是一个 List 或者它可以是一个 List.

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天全站免登陆