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

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

Syntax:

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

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

Parameters: Two parameters are required for this method.

a: the array to be filled.

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

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Arrays;

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

        byte a[] = new byte[5];

        byte val = 1;

        Arrays.fill(a, val);

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

Output:

[1, 1, 1, 1, 1]


No comments:

Post a Comment