26

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?

2
  • 4
    A 10 dimensional array : int[][][][][][][][][][] array; is not the same thing as a 1 dimensional array with 10 elements : new int[10]. Commented Mar 22, 2011 at 12:53
  • 8
    In his defense you could represent a 10 dimensional vector with 10 elements. Granted the terminology is off but the spirit of the question is clear. Commented Mar 22, 2011 at 12:59

3 Answers 3

56

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).

Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

And a ten-dimensional array of size 10 in all dimensions would require 37GB of memory, if I calculated correctly.
I calculate 10^9 * (8 (reference to) + 16 (array header) + 10*4) which is ~ 60 GB. ;)
Orrrrr... it's a ten-dimensional array that can hold 32 enumerable states :)
2

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

2 Comments

The REAL authority is JLS 15.10.1 (paragraph 5).
I looked but couldn't find a link very quickly and figured Oracle was enough of an authority; thanks for the spec though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.