Assign a Value to Every Element in an Array?


#1

Can someone tell me how I could assign a value to every element in an array with a single line of code? Is it even possible? Right now i have this,
yposi[0] = 0;
yposi[1] = 0;
yposi[2] = 0;
yposi[3] = 0;
yposi[4] = 0;

but is it possible to turn all of that into a single line of code which assigns 0 to every element in the array?

Thanks, Alex.


#2

It’s actually 2 lines, one for the import and one for the actual command:

import java.util.Arrays;

int[] yposi = new int[5];

Arrays.fill(yposi, 13); // <--- this

#3

Thanks, super helpful. This is exactly what I was looking for.


#4
final int QTY = 5;
final int[] ys = new int[QTY];

{
  java.util.Arrays.fill(ys, MAX_INT); // 1 statement w/o import
  println(ys);
  exit();
}