Fill value to array

static void fill(boolean[] a, boolean val)
Assigns the specified boolean value to each element of the specified array of booleans.
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
Assigns the specified boolean value to each element of the specified range of the specified array of booleans.
static void fill(byte[] a, byte val)
Assigns the specified byte value to each element of the specified array of bytes.
static void fill(byte[] a, int fromIndex, int toIndex, byte val)
Assigns the specified byte value to each element of the specified range of the specified array of bytes.
static void fill(char[] a, char val)
Assigns the specified char value to each element of the specified array of chars.
static void fill(char[] a, int fromIndex, int toIndex, char val)
Assigns the specified char value to each element of the specified range of the specified array of chars.
static void fill(double[] a, double val)
Assigns the specified double value to each element of the specified array of doubles.
static void fill(double[] a, int fromIndex, int toIndex, double val)
Assigns the specified double value to each element of the specified range of the specified array of doubles.
static void fill(float[] a, float val)
Assigns the specified float value to each element of the specified array of floats.
static void fill(float[] a, int fromIndex, int toIndex, float val)
Assigns the specified float value to each element of the specified range of the specified array of floats.
static void fill(int[] a, int val)
Assigns the specified int value to each element of the specified array of ints.
static void fill(int[] a, int fromIndex, int toIndex, int val)
Assigns the specified int value to each element of the specified range of the specified array of ints.
static void fill(long[] a, int fromIndex, int toIndex, long val)
Assigns the specified long value to each element of the specified range of the specified array of longs.
static void fill(long[] a, long val)
Assigns the specified long value to each element of the specified array of longs.
static void fill(Object[] a, int fromIndex, int toIndex, Object val)
Assigns the specified Object reference to each element of the specified range of the specified array of Objects.
static void fill(Object[] a, Object val)
Assigns the specified Object reference to each element of the specified array of Objects.
static void fill(short[] a, int fromIndex, int toIndex, short val)
Assigns the specified short value to each element of the specified range of the specified array of shorts.
static void fill(short[] a, short val)
Assigns the specified short value to each element of the specified array of shorts.

  import java.util.Arrays;

public class Main{
  public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++){
      array[i] = -3 * i;
    }
    System.out.print("Original contents: ");
    System.out.println(Arrays.toString(array));
    // fill and display
    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    System.out.println(Arrays.toString(array));
  }
}
  

The output:


Original contents: [0, -3, -6, -9, -12, -15, -18, -21, -24, -27]
After fill(): [0, -3, -1, -1, -1, -1, -18, -21, -24, -27]

import java.util.Arrays;

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

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

import java.util.Arrays;

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);

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

The output:


[true, true, true, true, false, false, false, false, false, false]
Home 
  Java Book 
    Collection  

Arrays:
  1. Arrays
  2. Convert array to list
  3. Binary search an Array
  4. Deep to string
  5. Compare two arrays
  6. Fill value to array
  7. Sort an array
  8. Convert array to string