当你做一个ArrayList不指定对象类型,它自动创建它,当你添加的第一个对象? [英] When you make an ArrayList without specifying the object type, does it create it automatically when you add the first object?
本文介绍了当你做一个ArrayList不指定对象类型,它自动创建它,当你添加的第一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,而不是做
ArrayList<ClassName> variableName;
您
ArrayList variableName;
然后再添加类型的对象类名
then later you add an object of type "ClassName"
variableName.add(objectName);
会自动设置你的阵列的类型为
will that automatically set the type of your array as
ArrayList<ClassName>
推荐答案
没有。泛型仅用于编译时。你只是失去了检查的好处。在运行时,所有的通用信息将被删除
No. Generics are for compile time only. You are just losing the benefit of that check. At runtime all generic information is erased
在换句话说,
ArrayList<Type>
在运行时只是一个ArrayList。这样做,经过短短一个List的好处是,当你写你的code,编译器会检查你不把任何东西不合适在列表中。
at runtime is just an ArrayList. The benefit of doing that over just a List is that when you are writing your code, the compiler will check that you dont put anything inappropriate in your list.
这篇关于当你做一个ArrayList不指定对象类型,它自动创建它,当你添加的第一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文