Java中的对象数组是否具有默认值?

时间:2022-06-28 07:30:40

If I construct:

如果我构建:

Object[][] guy = new Object[5][4];

do the spots in my array have default values? Are they null?

我的数组中的斑点有默认值吗?他们是空的吗?

Is there a way to assign default values for every spot in the array?

有没有办法为数组中的每个点分配默认值?

6 个解决方案

#1


9  

Yes, fields in new arrays are initialized with null in Java.

是的,新数组中的字段在Java中初始化为null。

You can use the method Arrays.fill() to fill all fields in an array with a specific value.

您可以使用方法Arrays.fill()来填充具有特定值的数组中的所有字段。

If you have arrays of short length where you statically know what to put it, you can use array initializers:

如果你有一个短长度的数组,你静态地知道放什么,你可以使用数组初始化器:

Object[][] guy = { { obj1, obj2, null, obj3 }, { ... }, ... };

You have to type out the full array with all fields (20 in your case) for that, so if you want to put the same value in every place, fill() is probably more convenient.

您必须输入包含所有字段的完整数组(在您的情况下为20),因此如果您想在每个地方放置相同的值,fill()可能更方便。

Arrays of primitive types btw. are initialized with the various variants of 0 and with false for boolean arrays (as long as you don't use an initializer). The rules for array initialization are the same as for the initialization of fields, and can be found here in the Java Language Specification.

原始类型的数组btw。使用0的各种变体进行初始化,对于布尔数组使用false进行初始化(只要不使用初始化程序)。数组初始化的规则与字段初始化的规则相同,可以在Java语言规范中找到。

#2


8  

The existing answers are all correct, but there is one bit to add in your particular example. Although reference arrays are defaulted to null, multi-dimensional arrays have the first dimension implictly created.

现有答案都是正确的,但在您的特定示例中还有一点要添加。尽管参考数组默认为null,但多维数组具有隐含地创建的第一维。

For example, in your case the first dimension wont be null, but will in fact point to another array:

例如,在您的情况下,第一个维度不为null,但实际上将指向另一个数组:

Object[][] guy = new Object[5][4];
System.out.println(guy[0]);
--> Output will be [Ljava.lang.Object;@5e743399

#3


1  

In Java, objects initializations assume the default value "null".

在Java中,对象初始化假定默认值为“null”。

#4


1  

All variables/fields are initialized in Java.

所有变量/字段都在Java中初始化。

Consult http://docs.oracle.com/javase/specs/

请访问http://docs.oracle.com/javase/specs/

#5


1  

Primitive types are initialized by default values, Referenced types are NOT initialised (so they are null)

原始类型由默认值初始化,引用类型未初始化(因此它们为空)

Try to run code snippet to see what happens public class ArraysSample {

尝试运行代码片段以查看公共类ArraysSample发生了什么{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Object[][] objAr = new Object[4][5];
    for (Object[] objects : objAr) {
        for (Object object : objects) {
            System.out.print(object + "\t");
        }
        System.out.println();
    }

    int[][] primitievArray = new int[4][5];
    for (int[] is : primitievArray) {
        for (int i : is) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

}

}

}


null null null null null
null null null null null
null null null null null
null null null null null
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

null null null null null null null null null null null null null null null null null 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

#6


0  

Yes you can have default assigning, for instance :

是的,您可以进行默认分配,例如:

instead of:

代替:

char[][] c = new char[5][4];

you can:

您可以:

char[][] c = {{'a','b','c','x','B'}, {'A','Z','w','Z','S'},
{'A','Z','w','Z','S'},{'A','Z','w','Z','S'}};

#1


9  

Yes, fields in new arrays are initialized with null in Java.

是的,新数组中的字段在Java中初始化为null。

You can use the method Arrays.fill() to fill all fields in an array with a specific value.

您可以使用方法Arrays.fill()来填充具有特定值的数组中的所有字段。

If you have arrays of short length where you statically know what to put it, you can use array initializers:

如果你有一个短长度的数组,你静态地知道放什么,你可以使用数组初始化器:

Object[][] guy = { { obj1, obj2, null, obj3 }, { ... }, ... };

You have to type out the full array with all fields (20 in your case) for that, so if you want to put the same value in every place, fill() is probably more convenient.

您必须输入包含所有字段的完整数组(在您的情况下为20),因此如果您想在每个地方放置相同的值,fill()可能更方便。

Arrays of primitive types btw. are initialized with the various variants of 0 and with false for boolean arrays (as long as you don't use an initializer). The rules for array initialization are the same as for the initialization of fields, and can be found here in the Java Language Specification.

原始类型的数组btw。使用0的各种变体进行初始化,对于布尔数组使用false进行初始化(只要不使用初始化程序)。数组初始化的规则与字段初始化的规则相同,可以在Java语言规范中找到。

#2


8  

The existing answers are all correct, but there is one bit to add in your particular example. Although reference arrays are defaulted to null, multi-dimensional arrays have the first dimension implictly created.

现有答案都是正确的,但在您的特定示例中还有一点要添加。尽管参考数组默认为null,但多维数组具有隐含地创建的第一维。

For example, in your case the first dimension wont be null, but will in fact point to another array:

例如,在您的情况下,第一个维度不为null,但实际上将指向另一个数组:

Object[][] guy = new Object[5][4];
System.out.println(guy[0]);
--> Output will be [Ljava.lang.Object;@5e743399

#3


1  

In Java, objects initializations assume the default value "null".

在Java中,对象初始化假定默认值为“null”。

#4


1  

All variables/fields are initialized in Java.

所有变量/字段都在Java中初始化。

Consult http://docs.oracle.com/javase/specs/

请访问http://docs.oracle.com/javase/specs/

#5


1  

Primitive types are initialized by default values, Referenced types are NOT initialised (so they are null)

原始类型由默认值初始化,引用类型未初始化(因此它们为空)

Try to run code snippet to see what happens public class ArraysSample {

尝试运行代码片段以查看公共类ArraysSample发生了什么{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Object[][] objAr = new Object[4][5];
    for (Object[] objects : objAr) {
        for (Object object : objects) {
            System.out.print(object + "\t");
        }
        System.out.println();
    }

    int[][] primitievArray = new int[4][5];
    for (int[] is : primitievArray) {
        for (int i : is) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

}

}

}


null null null null null
null null null null null
null null null null null
null null null null null
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

null null null null null null null null null null null null null null null null null 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

#6


0  

Yes you can have default assigning, for instance :

是的,您可以进行默认分配,例如:

instead of:

代替:

char[][] c = new char[5][4];

you can:

您可以:

char[][] c = {{'a','b','c','x','B'}, {'A','Z','w','Z','S'},
{'A','Z','w','Z','S'},{'A','Z','w','Z','S'}};