fill() – fill array

This Java code defines a class named ArrayMethods that contains a main method.

The main method creates an array myArray and initializes it with the values {6, 4, 9, 1, 3, 7}.

Next, the code calls the Arrays.fill method to fill the entire array myArray with the value 1. The Arrays.fill method modifies the original array in place and sets all elements of the array to the specified value.

Finally, the code outputs the filled array by using a for loop to iterate over the elements of the array and printing each element followed by a space. The code calls System.out.print with the current array element and the string ” ” to print each element. The for loop continues until it has iterated over all elements of the array.

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