这两件事是一样的吗?
for(int i=0; i<array.length; i++){
array[i] = null;
}
Run Code Online (Sandbox Code Playgroud)
和
array = null;
Run Code Online (Sandbox Code Playgroud)
cod*_*ict 17
一个显示差异的小片段:
// declare a array variable that can hold a reference.
String [] array;
// make it null, to indicate it does not refer anything.
array = null;
// at this point there is just a array var initialized to null but no actual array.
// now allocate an array.
array = new String[3];
// now make the individual array elements null..although they already are null.
for(int i=0;i<array.length;i++)
{
array[i] = null;
}
// at this point we have a array variable, holding reference to an array,
// whose individual elements are null.
Run Code Online (Sandbox Code Playgroud)
不,它不一样.
事实上,为了正确运行第一段代码,应该像这样声明和初始化数组变量(例如)
Object[] array = new Object[5];
Run Code Online (Sandbox Code Playgroud)
这将创建一个包含5个元素的数组,每个元素都有一个null值.
一旦你有了这个,在第一个例子中你正在做的是为每个array[i]元素分配一个空值.array不会null.所以你应该有这样的东西.
数组 - >
通过这样做,array = null你说数组不再引用该元素数组.现在你应该有
array - > null
| 归档时间: |
|
| 查看次数: |
57206 次 |
| 最近记录: |