Arrays.fill(int[], int) in Java

Arrays.fill(int[], int): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.fill(int[] a, int val)

This method takes two arguments one of type int array and the other of type int as its parameters. This method assigns the specified int value to each element of the specified array of ints.

Parameters: Two parameters are required for this method.

a: the array to be filled.

val: the value to be stored in all elements of the array.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Arrays;

public class Arraysfillint {
    public static void main(String[] args) {

        int a[] = new int[5];

        int val = 10;

        Arrays.fill(a, val);

        System.out.println(Arrays.toString(a));
    }
}

Output:

[10, 10, 10, 10, 10]


No comments:

Post a Comment