安卓:发行期间ArrayList的声明 [英] Android: Issue during Arraylist declaration
问题描述
如果我宣布ArrayList中像这样 -
If I declare Arraylist like this-
private ArrayList<Integer[]> nodeList;
那么,同时加入数组进去,得到NullPointerException异常
then, while adding array into it, getting NullPointerException
但是,如果我把它改为 -
But, if I change it to-
private ArrayList<Integer[]> nodeList= new ArrayList<Integer[]>();
- 它工作得很好。
-it works fine.
为什么第一个失败!
推荐答案
第一只声明了一个变量,但不会产生实际的对象。只有当你使用新
,您实际创建的对象。
The first only declares a variable, but does not create the actual object. only when you use new
, you actually create the object.
在的Java与C ++,声明一个变量不分配它的局部变量。要真正创建对象,你需要明确创建它[在你的榜样:通过使用新
关键字。
(*)请注意,这是唯一真正要的引用类型的对象,和Java 元与申报创建。
In java unlike C++, declaring a variable does not allocate a local variable of it. To actually create the object, you need to explicitly create it [in your example: by using the new
keyword].
(*)Note that this is only true to reference types objects, and java primitives are created with declaration.
这篇关于安卓:发行期间ArrayList的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!