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

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

Syntax:

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

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

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 Arraysfillboolean {
    public static void main(String[] args) {

        boolean a[] = new boolean[5];

        boolean val = true;

        Arrays.fill(a, val);

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

Output:

[true, true, true, true, true]


No comments:

Post a Comment