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

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

Syntax:

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

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

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

        char a[] = new char[5];

        char val = 'a';

        Arrays.fill(a, val);

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

Output:

[a, a, a, a, a]


No comments:

Post a Comment