java 创建引用数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15064457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create an array of references
提问by Akash
If I have a class say A, and I declare an array of 10 elements of this class as,
如果我有一个类说 A,并且我将这个类的 10 个元素的数组声明为,
A [] arr=new A[10];
A [] arr=new A[10];
Then 10 new objects of A are created and stored in the array.
然后 A 的 10 个新对象被创建并存储在数组中。
However, i'd like to be able to do something on the lines of A arr[10];
, where the array just holds references to null objects.
但是,我希望能够在 的行上做一些事情A arr[10];
,其中数组只保存对空对象的引用。
The reason I need this is because I only need the array to hold instances I fill in later in the code. So the objects the above statement creates are lost anyway and as I understand object creation is expensive.
我需要这个的原因是因为我只需要数组来保存我稍后在代码中填写的实例。所以上面语句创建的对象无论如何都会丢失,据我所知,对象创建是昂贵的。
So, is there any way to just have an array of references that I can point to the objects I desire later? Or is this not possible and I should resort to using an ArrayList
?
那么,有没有什么办法可以让一个引用数组指向我以后想要的对象?或者这是不可能的,我应该求助于使用ArrayList
?
回答by NPE
If I have a class say
A
, and I declare an array of 10 elements of this class as,A [] arr=new A[10];
Then 10 new objects of A are created and stored in the array.
如果我有一个类 say
A
,并且我将这个类的 10 个元素的数组声明为,A [] arr=new A[10];
然后 A 的 10 个新对象被创建并存储在数组中。
That's not correct. Here, an array of ten referencesgets created, and each reference gets set to null
. There are no instances of A
created by this code.
那不正确。在这里,创建了一个包含 10 个引用的数组,并且每个引用都设置为null
。没有A
此代码创建的实例。
In other words, the code already does exactly what you'd like it to do.
换句话说,代码已经完全按照您的意愿去做了。
回答by Peter
if you do A [] arr=new A[10];
then no objects are created except your array, each field will be null
until you initialize it.
如果你这样做,A [] arr=new A[10];
那么除了你的数组之外不会创建任何对象,每个字段都将null
在你初始化它之前。
A [] arr=new A[10];
only create place to store reference of A
Class object. Although array arr
is created but its not referencing any object and you can't do like arr[i].someMethod()
.
A [] arr=new A[10];
只创建地方来存储A
Class 对象的引用。虽然arr
创建了数组但它没有引用任何对象,你不能像arr[i].someMethod()
.
To correct it, allocate object at individual memory in array do like this:
要纠正它,请在数组中的单个内存中分配对象,请执行以下操作:
A [] arr=new A[10];
arr[0] = new A();
arr[1] = new A();
:
:
or in a loop like:
或在一个循环中:
for(i=0; i<10; i++){
arr[i] = new A();
}
After there arr
that is an array of reference, refers to a valid A class object. And after this expression arr[i].someMethod()
will not cause an error.
在那里之后arr
是一个引用数组,指的是一个有效的 A 类对象。并且在这个表达式之后arr[i].someMethod()
不会导致错误。
回答by Sumit Singh
Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). The array components effectively cease to exist when the array is no longer referenced.
数组组件是未命名的变量,每当创建作为数组的新对象(第 10节、第15.10节)时,就会创建并初始化为默认值(第4.12.5节)。当不再引用数组时,数组组件实际上不复存在。
And from 4.12.5. Initial Values of Variables
For all reference types (§4.3), the default value is null.
对于所有引用类型(第 4.3 节),默认值为null。
So as you says:
所以正如你所说:
If I have a class say A, and I declare an array of 10 elements of this class as,
A [] arr=new A[10];
Then 10 new objects of A are created and stored in the array.
如果我有一个类说 A,并且我将此类的 10 个元素声明为
A [] arr=new A[10];
然后 A 的 10 个新对象被创建并存储在数组中。
That's not correct. But what you want is correct .
那不正确。但你想要的是正确的。
So, is there any way to just have an array of references that I can point to the objects I desire later?
那么,有没有什么办法可以让一个引用数组指向我以后想要的对象?