Create a zero int array in Java

How do I initialize all elements of an array to 0?

for example

int[] array = new int[10];
return array[2];

Should return 0, not null

Solution

Int always has an initial value of 0

new int[10]

Enough

Use the array utility class for other values

int arrayDefaultedToTen[] = new int[100]; 

   Arrays.fill(arrayDefaultedToTen,10);

This method fills the array (first ARG) with 10 (second ARG)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>