Java Arrays fill array from start index to end index

Description

Java Arrays fill array from start index to end index

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    int[] integer = new int[10];
    System.out.println(Arrays.toString(integer));
    /*from w  w w  .jav a2  s .c om*/
    Arrays.fill(integer, 1, 4, 5);
    System.out.println(Arrays.toString(integer));
    
    
    int startIndex = 0;
    int endIndex = 2;
    int value = -1;
    Arrays.fill(integer, startIndex, endIndex, value);
    System.out.println(Arrays.toString(integer));
  }
}

Fill boolean value

import java.util.Arrays;

public class Main {
  public static void main(String[] argv) throws Exception {

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

    System.out.println(Arrays.toString(booleanArr));
    //from   w w  w .jav  a  2s.co  m
    int startIndex = 0;
    int endIndex = 4;
    booleanFillValue = false;

    Arrays.fill(booleanArr, startIndex, endIndex, booleanFillValue);
    System.out.println(Arrays.toString(booleanArr));
  }
}



PreviousNext

Related