当你在没有指定对象类型的情况下创建一个 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;
然后你添加一个类型为ClassName"的对象
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 相比,这样做的好处是,当您编写代码时,编译器会检查您是否没有在列表中放入任何不合适的内容.
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屋!
查看全文