Example usage for java.util Arrays fill

List of usage examples for java.util Arrays fill

Introduction

In this page you can find the example usage for java.util Arrays fill.

Prototype

public static void fill(Object[] a, int fromIndex, int toIndex, Object val) 

Source Link

Document

Assigns the specified Object reference to each element of the specified range of the specified array of Objects.

Usage

From source file:Main.java

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

  int startIndex = 0;
  int endIndex = 4;
  double[] doubleArr = new double[10];
  double doubleFillValue = 1;
  Arrays.fill(doubleArr, startIndex, endIndex, doubleFillValue);
}

From source file:Main.java

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

        int startIndex = 0;
        int endIndex = 4;

        String[] stringArr = new String[10];
        String stringFillValue = "1";

        Arrays.fill(stringArr, startIndex, endIndex, stringFillValue);

    }/*w  w w.  java  2 s.  com*/

From source file:Main.java

public static int[] replaceArray(int[] arr, int startIndex, int endIndex, int value) {
    Arrays.fill(arr, startIndex, endIndex, value);
    return arr;//from ww w  .java2  s .  co m
}

From source file:Main.java

public static void setViewPointMatrix(float matrix[], float x, float y, float z) {
    // The matrix is
    // -z,  0,  x,  0
    //  0, -z,  y,  0
    //  0,  0,  1,  0
    //  0,  0,  1, -z
    Arrays.fill(matrix, 0, 16, 0);
    matrix[0] = matrix[5] = matrix[15] = -z;
    matrix[8] = x;//from ww  w .  j  a va2 s.c  o  m
    matrix[9] = y;
    matrix[10] = matrix[11] = 1;
}

From source file:Main.java

public static double[] padWithZeros(double[] input, int len, double[] output) {
    if (len <= input.length) {
        return input;
    } else {/* ww w  .ja va2 s . com*/
        if (output == null || output.length != len) {
            output = new double[len];
        }
        System.arraycopy(input, 0, output, 0, input.length);
        Arrays.fill(output, input.length, output.length, 0);
        return output;
    }
}

From source file:Main.java

private static byte[] expand(byte[] bytes, int skip) {
    byte[] newBytes = new byte[bytes.length - skip];
    Inflater inflater = new Inflater();

    inflater.setInput(bytes, skip, newBytes.length);
    try {//from  w  w w  . j  av a2  s .c o m
        int outCount = inflater.inflate(newBytes);
        System.arraycopy(newBytes, 0, bytes, skip, outCount);
        Arrays.fill(bytes, skip + outCount, bytes.length, (byte) 0);
        return bytes;
    } catch (DataFormatException e) {
    }

    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] grow(T[] array, int length, T fillElement) {
    Class<T> componentType = (Class<T>) array.getClass().getComponentType();
    T[] newArray = (T[]) Array.newInstance(componentType, length);
    System.arraycopy(array, 0, newArray, 0, array.length);
    Arrays.fill(newArray, array.length, newArray.length, fillElement);
    return newArray;
}

From source file:Main.java

public static void readTillEnd(InputStream in, byte[] buf, boolean eofOK) throws IOException {
    int toRead = buf.length;
    int numRead = 0;
    while (numRead < toRead) {
        int nread = in.read(buf, numRead, toRead - numRead);
        if (nread < 0) {
            if (eofOK) {
                // EOF hit, fill with zeros
                Arrays.fill(buf, numRead, toRead, (byte) 0);
                numRead = toRead;/*from ww w.  j a va2  s . c  om*/
            } else {
                // EOF hit, throw.
                throw new IOException("Premature EOF");
            }
        } else {
            numRead += nread;
        }
    }
}

From source file:Main.java

public static boolean[] grow(boolean[] array, int length, boolean fillElement) {
    boolean[] newArray = new boolean[length];
    System.arraycopy(array, 0, newArray, 0, array.length);
    Arrays.fill(newArray, array.length, newArray.length, fillElement);
    return newArray;
}

From source file:Main.java

/**
 * Ensures the given array has a given size.
 * @param array        the array./*from w  w w.j a v  a2s . c o  m*/
 * @param size         the target size of the array.
 * @param initialValue the initial value of the elements.
 * @return             the original array, or a copy if it had to be
 *                     extended.
 */
public static boolean[] ensureArraySize(boolean[] array, int size, boolean initialValue) {
    // Is the existing array large enough?
    if (array.length >= size) {
        // Reinitialize the existing array.
        Arrays.fill(array, 0, size, initialValue);
    } else {
        // Otherwise create and initialize a new array.
        array = new boolean[size];

        if (initialValue) {
            Arrays.fill(array, 0, size, initialValue);
        }
    }

    return array;
}