Any shortcut to initialize all array elements to zero?

后端 未结 14 2755
北恋
北恋 2020-11-28 18:12

In C/C++ I used to do

int arr[10] = {0};

...to initialize all my array elements to 0.

Is there a similar shortcut in

14条回答
  •  隐瞒了意图╮
    2020-11-28 18:42

    A default value of 0 for arrays of integral types is guaranteed by the language spec:

    Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0.  

    If you want to initialize an one-dimensional array to a different value, you can use java.util.Arrays.fill() (which will of course use a loop internally).

提交回复
热议问题