什么是一个空数组和空数组之间的区别? [英] What is the difference between a null array and an empty array?
问题描述
如果一个int数组中的单个元素不被初始化,什么是默认存储在其中?我似乎发现有类似空数组或一个空数组。有什么区别,哪一个适用于我的第一个问题?
If the individual elements of an int array are not initialized, what is stored in them by default? I apparently found that there is something like an empty array or a null array. What is the difference, and which one applies to my first question?
推荐答案
从技术上讲,有作为的空数组的没有这样的事;但因为数组是对象,数组类型是引用类型(即:数组变量牵住的引用的到阵列),这意味着数组变量可以是空
,而不是实际指向数组:
Technically speaking, there's no such thing as a null array; but since arrays are objects, array types are reference types (that is: array variables just hold references to arrays), and this means that an array variable can be null
rather than actually pointing to an array:
int[] notAnArray = null;
这是空的数组零长度的数组;它没有元素:
An empty array is an array of length zero; it has no elements:
int[] emptyArray = new int[0];
(可以的从不的有元素,因为它的创建后数组的长度不会改变)。
(and can never have elements, because an array's length never changes after it's created).
当你创建的不的没有为它的元素指定值-empty阵列,它们默认为零样值— 0
为一个整数数组,空
为对象类型等的数组;所以,这样的:
When you create a non-empty array without specifying values for its elements, they default to zero-like values — 0
for an integer array, null
for an array of an object type, etc.; so, this:
int[] arrayOfThreeZeroes = new int[3];
是相同的,因为这
is the same as this:
int[] arrayOfThreeZeroes = { 0, 0, 0 };
(尽管这些值可以重新之后分配;数组的长度不能改变,但它的内容可以改变)。
(though these values can be re-assigned afterward; the array's length cannot change, but its elements can change).
这篇关于什么是一个空数组和空数组之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!