fill() – fill array

Assigns the specified int value to each element of the specified array of ints.

Code Explanation
LineDescription
5Declaration and initialization of the array myArray with the values 6, 4, 9, 1, 3 and 7.
7The fill() assigns the specified int value (1) to each element of myArray
9 – 11With a for loop, each array element is output by specifying the index.
import java.util.Arrays;

public class ArrayMethods {
    public static void main(String[] args) {
        int[] myArray = { 6, 4, 9, 1, 3, 7 };

        Arrays.fill(myArray, 1);

        for (int i = 0; i < myArray.length; i++) {
            System.out.print(myArray[i] + " ");
        }
    }
}
Output
1 1 1 1 1 1