Christian
Christian

Reputation: 26437

Creating an int array filed with zeros in Java

I want to create a 10 dimensional array that's filled with zeros. If I simply use int[] array = new int[10]; do I have a guarantee that all int's in the array are zeros?

Upvotes: 26

Views: 71056

Answers (3)

Kerem Baydoğan
Kerem Baydoğan

Reputation: 10720

int always has initial value of 0. so

new int[10] 

is enough.

for other values use Arrays utility class.

   int arrayDefaultedToTen[] = new int[100]; 

   Arrays.fill(arrayDefaultedToTen, 10);

this method fills the array (first arg) with 10 (second arg).

Upvotes: 56

Andrew White
Andrew White

Reputation: 53526

Doing a new int[10] will be plenty. Refer to the authority for the default values.

Upvotes: 2

Vance Maverick
Vance Maverick

Reputation: 822

Yes, but it's only one-dimensional, not ten.

Upvotes: 3

Related Questions