Java Collection How to - Fill Elements in an Array








Question

We would like to know how to fill Elements in an Array.

Answer

import java.util.Arrays;
// w  ww.ja v a 2s.c o  m
public class Main {
  public static void main(String[] argv) throws Exception {

    boolean[] booleanArr = new boolean[10];
    boolean booleanFillValue = false;
    Arrays.fill(booleanArr, booleanFillValue);
  }
}

Fill to a continuous range of elements in an array

import java.util.Arrays;
//  w  ww. j a va 2s .  c  o m
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;

    boolean[] booleanArr = new boolean[10];
    boolean booleanFillValue = true;

    Arrays.fill(booleanArr, startIndex, endIndex, booleanFillValue);

  }
}