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, Object val) 

Source Link

Document

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

Usage

From source file:Main.java

public static String[] fillArray(String str, int length) {
    String result[] = new String[length];
    Arrays.fill(result, str);
    return result;
}

From source file:Main.java

public static boolean[] is_prime_up_to_with_sieve(int num) {
    boolean[] prime = new boolean[num + 1];
    Arrays.fill(prime, true);
    prime[0] = false;/* w ww  . j av  a2 s.  c  o  m*/
    prime[1] = false;
    int num_sqrt = (int) Math.ceil(Math.sqrt(num));

    for (int i = 2; i <= num_sqrt; i++) {
        if (prime[i])
            for (int j = i * i; j <= num; j += i)
                prime[j] = false;
    }
    return prime;
}

From source file:Main.java

public static String repeat(String s, int n) {
    if (s.length() == 1) {
        byte[] bb = new byte[n];
        Arrays.fill(bb, s.getBytes()[0]);
        return new String(bb);
    } else {//  w w  w  .ja  v  a  2  s  .  c  o m
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < n; i++)
            ret.append(s);
        return ret.toString();
    }
}

From source file:Main.java

public static void orderRandom(int upto, int order[]) {

    Arrays.fill(order, -1);

    for (int i = 0; i < upto;) {
        int val = randomInt(upto);
        if (!intExists(order, val, upto)) {
            order[i] = val;
            i++;/*from w w  w  .j av a  2s .c  om*/
        }
    }

}

From source file:Main.java

public static String prependString(String str, int len) {
    try {/*from ww w. j av a 2s  . com*/
        char[] tmp = new char[len - str.length()];
        Arrays.fill(tmp, ' ');
        return new String(tmp) + str;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static double[] sobelKernel(int length, int size) {
    if (size <= 0) {
        return null;
    }/*from  w  w w.j  av  a2s . c o m*/
    double[] sobelKernel = new double[length];
    Arrays.fill(sobelKernel, 0);
    for (int i = 1; i <= size; i++) {
        sobelKernel[length - i] = -i;
        sobelKernel[i] = i;
    }
    return sobelKernel;
}

From source file:Main.java

public static Class[] params(Class cls, int times) {
    Class[] classes = new Class[times];
    Arrays.fill(classes, cls);
    return classes;
}

From source file:Main.java

public static double[] triangleKernel(int length, int size) {
    if (size <= 0) {
        return null;
    }//from www .java 2s  .c o  m
    double[] triangleKernel = new double[length];
    Arrays.fill(triangleKernel, 0);
    triangleKernel[0] = 1;
    for (int i = 1; i <= size; i++) {
        triangleKernel[length - i] = 1 - (double) i / (double) size;
        triangleKernel[i] = 1 - (double) i / (double) size;
    }
    return triangleKernel;
}

From source file:Main.java

public static byte[] createZeroBytes(int length) {
    if (length <= 0)
        throw new IllegalArgumentException("length must be gt 0");

    byte[] bytes = new byte[length];
    Arrays.fill(bytes, (byte) 0);
    return bytes;
}

From source file:Main.java

static String toBase58(final byte[] in) {

    final int[] indexes = new int[128];

    Arrays.fill(indexes, -1);

    final char[] ab = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();

    for (int k = 0; k < ab.length; ++k)
        indexes[ab[k]] = k;/*from  w w w . j ava  2 s. co m*/

    int zeroCounter = 0;

    while (zeroCounter < in.length && in[zeroCounter] == 0)
        ++zeroCounter;

    final byte[] cp = Arrays.copyOf(in, in.length);

    final char[] enc = new char[cp.length * 2];

    int resBegin = enc.length;
    int begin = zeroCounter;

    while (begin < cp.length) {

        int rem = 0;
        for (int j = begin; j < cp.length; ++j) {
            final int temp = ((int) cp[j] & 0xFF) + (256 * rem);
            cp[j] = (byte) (temp / 58);
            rem = temp % 58;
        }

        enc[--resBegin] = ab[(byte) rem];
        if (cp[begin] == 0)
            ++begin;
    }

    while (resBegin < enc.length && ab[0] == enc[resBegin])
        ++resBegin;

    while (--zeroCounter >= 0)
        enc[--resBegin] = ab[0];

    return new String(enc, resBegin, enc.length - resBegin);
}