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?
If all adjacent elements(i, i+1) in array are equal and total number of element in array is even then it's all element can be converted to zero. For example, if array elements are like {1, 1, 2, 2, 3, 3} then its all element is convertible into zero.
You can call it like this: //fixed arrays int a[10]; setValue(a, 0); //dynamic arrays int *d = new int[length]; setValue(d, length, 0);
Java allows creating an array of size zero. If the number of elements in a Java array is zero, the array is said to be empty. In this case you will not be able to store any element in the array; therefore the array will be empty.
Use the fill() method to create an array filled with zeros, e.g. new Array(3). fill(0) , creates an array containing 3 elements with the value of 0 . The fill() method sets the elements in an array to the provided value and returns the modified array.
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).
Yes, but it's only one-dimensional, not ten.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With