How can i autofill an array (specially multidimensional) in java with ‘-1’ without using any loops..?
I know by default java initialize all the elements of array with ‘0’. But what if i want to replace all zeroes with -1 at once..! Without using any loop.
Is there any shortcut available..?
You can use Arrays.fill to fill a one dimensional array but you’ll have to loop for multi-dimensional arrays.
What’s your reason to avoid loops ? If you’re concerned with performances (and you’re sure there is a problem) you might do what is often done, that is flatten your array in a one-dimensional one :
then you can fill it with only one
fill(which hides a loop, though) and many manipulations would be a little more efficient. Accessing a cell would beflatarray[x+y*width].But you should profile first to ensure you need this.