Java Utility Library

Java Arrays - fill() Method



The java.util.Arrays.fill() method is used to assign the specified boolean value to each element of the specified array of booleans.

Syntax

public static void fill(boolean[] a, boolean val)

Parameters

a Specify the array to be filled.
val Specify the value to be stored in all elements of the array.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Arrays.fill() method is used to fill the boolean array with specified boolean value.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a boolean array
    boolean MyArr[] = {true, false, false, true, false};

    //printing array
    System.out.print("MyArr contains:"); 
    for(boolean i: MyArr)
      System.out.print(" " + i);

    //fill the array with true boolean value
    Arrays.fill(MyArr, true);

    //printing array
    System.out.print("\nMyArr contains:"); 
    for(boolean i: MyArr)
      System.out.print(" " + i);   
  }
}

The output of the above code will be:

MyArr contains: true false false true false
MyArr contains: true true true true true

❮ Java.util - Arrays