不要在Java对象数组有默认值? [英] Do object arrays in Java have default values?

查看:234
本文介绍了不要在Java对象数组有默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我建:

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?

推荐答案

是的,在新的领域数组初始化在Java空。

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

您可以使用方法<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#fill%28java.lang.Object%5B%5D,%20java.lang.Object%29\"相对=nofollow> Arrays.fill() 填补了一个特定值的阵列的所有领域。

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

如果你有短长数组,你知道静态放什么,你可以使用的数组的初始化对象的:

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 }, { ... }, ... };

您必须键入了所有的字段(在你的案件20)的全阵列,所以如果你想要把相同的值在每一个地方,填写()可能更方便。

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.

原始类型BTW的数组。为0的各种变种和初始化虚假布尔阵列(只要你不使用的初始化)。对于数组初始化的规则是相同的,作为场的初始化,可以发现此处在Java语言规范。

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.

这篇关于不要在Java对象数组有默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆