Java Array Fill fill(final Object[] array, final int start, final int end, final Object value)

Here you can find the source of fill(final Object[] array, final int start, final int end, final Object value)

Description

Fill the specified segment of an array with the provided value.

License

Apache License

Parameter

Parameter Description
array The array to fill. Must not be null.
start The first index of the array to be set to value inclusive.
end The last index of the array to be set to value exclusive.
value The value to put in the array.

Declaration

public static void fill(final Object[] array, final int start, final int end, final Object value) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    private static final int LINE_SIZE = 1024;

    /**//from   w  ww .  ja  v  a 2s. com
     * Fill the specified segment of an array with the provided value. Initializes a small amount of values
     * directly, larger values are them copied using {@link System#arraycopy(Object, int, Object, int, int)}
     * on the previous section in order to make sure the accessed values are in the L1 cache.
     *
     * @param array The array to fill. Must not be null.
     * @param start The first index of the array to be set to value inclusive.
     * @param end The last index of the array to be set to value exclusive.
     * @param value The value to put in the array.
     */
    public static void fill(final Object[] array, final int start, final int end, final Object value) {
        assert start >= 0 && start <= end;
        assert end <= array.length;
        assert array.length == 0 || array.length == pow2(array.length);
        final int n = LINE_SIZE;
        int i = Math.min(n, end);
        Arrays.fill(array, start, i, value);
        while (i < end) {
            System.arraycopy(array, i - n, array, i, n);
            i += n;
        }
    }

    public static int pow2(int c) {
        assert c > 0;
        c--;
        c |= c >> 1;
        c |= c >> 2;
        c |= c >> 4;
        c |= c >> 8;
        c |= c >> 16;
        c++;
        return c;
    }
}

Related

  1. fill(byte[] val, byte b)
  2. fill(char[] chars, int fromIndex, int toIndex, char c)
  3. fill(double[][] array, double s)
  4. fill(double[][] d, double val)
  5. fill(final double[][] arr, final double val)
  6. fill(final T[] array, final int size)
  7. fill(final T[] array, final T value)
  8. fill(int[][] array, int value)
  9. fill(String[] ary, String value)