的ArrayList< T> vs ArrayList<?> [英] ArrayList<T> vs ArrayList<?>

查看:93
本文介绍了的ArrayList< T> vs ArrayList<?>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用了 ArrayList< T> ,但是何时应该使用 ArrayList<> ?你能用例子来解释吗?谢谢。

I know what ArrayList<T> is used for, but when should I use ArrayList<?> ? can you explain with example? thanks.

推荐答案

据我所知, ArrayList<?> / code>基本上告诉编译器:

As far as I've been able to tell, ArrayList<?> basically tells the compiler:


是的,我知道有一个通用版本的ArrayList可供我使用,但我真的,真的不知道我期待在这一件物品中有什么样的物品。所以不要给我警告说我没有按照我应该的方式使用泛型。

Yes, I know that there is a generic version of ArrayList available to me, but I really, genuinely don't know what kind of objects I'm expecting to be in this one. So don't give me warnings that I'm not using generics the way I should be.



更新



我刚了解到使用原始类型( ArrayList )和带通配符的泛型( ArrayList< ;?> )超越避免编译器警告。显然,一旦你将某些东西声明为Raw类型,即使泛型不是基于你省略的类型,泛型也不能用于该类型的任何方法。 请参阅此处以获取示例

Update

I just learned that there is a real difference between using Raw Types (ArrayList) and generics with wildcards (ArrayList<?>) that goes beyond avoiding compiler warnings. Apparently once you declare something as a Raw type, no generics will work on any methods of that type, even if the generics weren't based on the type you omitted. See here for an example.

因此,虽然我的原始答案通常是正确的,但我认为使用 ArrayList <?>< / code>代替 ArrayList 不仅仅是删除编译器警告的问题。

So while my original answer was generally correct, I thought it would be important to mention that using ArrayList<?> instead of ArrayList is more than just a matter of removing compiler warnings.

这篇关于的ArrayList&LT; T&GT; vs ArrayList&lt;?&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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