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 Java?
I want to avoid using the loop, is it possible?
int arr[] = new int[10];
for(int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
A default value of 0 for arrays of integral types is guaranteed by the language spec:
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).