I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Using the fill() method The fill() method, fills the elements of an array with a static value from the specified start position to the specified end position. If no start or end positions are specified, the whole array is filled. One thing to keep in mind is that this method modifies the original/given array.
fill() method is in java. util. Arrays class. This method assigns the specified data type value to each element of the specified range of the specified array.
Array.fill For example, if we want to set up an array with ten slots and populate it with the string “hello” we'd write some code like this: let filledArray = new Array(10). fill('hello'); This method works great for immutable values like numbers, strings, and booleans.
Check out the Arrays.fill methods.
int[] array = new int[4]; Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
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