ArrayList<?>,ArrayList,ArrayList< Object&gt ;?有什么区别? [英] What is the difference between ArrayList<?>, ArrayList, ArrayList<Object>?

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

问题描述

有人可以解释一下 ArrayList <?>> ArrayList ArrayList< Object> 是什么时候使用每个?它们全都是相同的还是每个在实现级别上都有不同的含义? ArrayList< Object> ; 特别是 列表 Object s,而 ArrayList <?> 是一个我们不确定的具体类型的列表(意思是除了 null 之外,我们不能添加任何内容)。当列表的类型不相关时,你可以使用后者。当您要执行的操作不取决于列表的类型时。例如:

  public static boolean isBigEnough(ArrayList<?> list){
return list.size() > 42;
}

这些都覆盖在泛型教程(请参阅

最后, ArrayList 中没有类型参数的是原始类型:甚至允许它的唯一原因是为了向下兼容Java版本5以下,而且您应尽可能避免使用它。


Can someone please explain what the difference between ArrayList<?>, ArrayList and ArrayList<Object> is, and when to use each? Are they all same or does each have some different meaning at the implementation level?

解决方案

ArrayList<Object> is specifically a list of Objects whereas ArrayList<?> is a list whose concrete type we are unsure of (meaning we can't add anything to the list except null). You would use the latter when the list's type is irrelevant, e.g. when the operation you want to perform does not depend on the type of the list. For instance:

public static boolean isBigEnough(ArrayList<?> list) {
    return list.size() > 42;
}

This is all covered in the generics tutorial (see the wildcards section).

Finally, ArrayList with no type parameter is the raw type: the only reason it's even allowed is for backwards compatibility with Java versions under 5, and you should refrain from using it whenever possible.

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

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