Fill value to array

ReturnMethodSummary
static voidfill(boolean[] a, boolean val)Assigns the specified boolean value to each element of the specified array of booleans.
static voidfill(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 voidfill(byte[] a, byte val)Assigns the specified byte value to each element of the specified array of bytes.
static voidfill(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 voidfill(char[] a, char val)Assigns the specified char value to each element of the specified array of chars.
static voidfill(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 voidfill(double[] a, double val)Assigns the specified double value to each element of the specified array of doubles.
static voidfill(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 voidfill(float[] a, float val)Assigns the specified float value to each element of the specified array of floats.
static voidfill(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 voidfill(int[] a, int val)Assigns the specified int value to each element of the specified array of ints.
static voidfill(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 voidfill(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 voidfill(long[] a, long val)Assigns the specified long value to each element of the specified array of longs.
static voidfill(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 voidfill(Object[] a, Object val)Assigns the specified Object reference to each element of the specified array of Objects.
static voidfill(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 voidfill(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]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.