Java Collection Tutorial - Java Arrays.fill(boolean[] a, int fromIndex, int toIndex, boolean val)








Syntax

Arrays.fill(boolean[] a, int fromIndex, int toIndex, boolean val) has the following syntax.

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

Example

In the following code shows how to use Arrays.fill(boolean[] a, int fromIndex, int toIndex, boolean val) method.

//from w w  w.  j  a v  a  2  s .  c  o m
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    
    // initializing boolean array
    boolean arr[] = new boolean[] {true, true, true, true, true};
    
    System.out.println(Arrays.toString(arr));

    // using fill for placing false from index 1 to 4
    Arrays.fill(arr, 1, 4, false);

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

The code above generates the following result.