使数组的元素为空

新手上路,请多包涵

这两个是一样的东西吗?

 for(int i=0; i<array.length; i++){
array[i] = null;
}

array = null;

原文由 user69514 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 597
1 个回答

一个小片段来显示差异:

 // 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.

原文由 codaddict 发布,翻译遵循 CC BY-SA 2.5 许可协议

推荐问题