Example usage for java.util Arrays copyOf

List of usage examples for java.util Arrays copyOf

Introduction

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

Prototype

public static boolean[] copyOf(boolean[] original, int newLength) 

Source Link

Document

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

Usage

From source file:Main.java

public static <T> T[] concatAll(T[] first, T[]... rest) {
    int totalLength = first.length;
    for (T[] array : rest) {
        totalLength += array.length;//from  www.j a v  a  2 s  .  c o m
    }
    T[] result = Arrays.copyOf(first, totalLength);
    int offset = first.length;
    for (T[] array : rest) {
        System.arraycopy(array, 0, result, offset, array.length);
        offset += array.length;
    }
    return result;
}

From source file:Main.java

public static int[][] deepCopy(int[][] arg) {
    int[][] copy = null;
    if (arg != null) {
        copy = new int[arg.length][];
        for (int rowIndex = arg.length - 1; rowIndex >= 0; rowIndex--)
            copy[rowIndex] = Arrays.copyOf(arg[rowIndex], arg[rowIndex].length);
    }//from   w w  w.  java  2s . co m
    return copy;
}

From source file:Main.java

public static byte[] appendArrays(byte[]... arrays) {
    byte[] retArr = new byte[0];
    int pos = 0;//from  w  w w. j  a v  a 2s  .  c om
    for (byte[] arr : arrays) {
        pos = retArr.length;
        retArr = Arrays.copyOf(retArr, retArr.length + arr.length);
        for (int idx = 0; idx < retArr.length; idx++) {
            retArr[pos + idx] = arr[idx];
        }
    }
    return retArr;
}

From source file:Main.java

public static <T> T[] concatArray(T[] array1, T[] array2) {
    if (array1 == null || array1.length == 0) {
        return array2;
    }/*w  w  w  .  ja  v a  2 s . c  o  m*/
    if (array2 == null || array2.length == 0) {
        return array1;
    }
    T[] result = Arrays.copyOf(array1, array1.length + array2.length);
    System.arraycopy(array2, 0, result, array1.length, array2.length);
    return result;
}

From source file:Main.java

public static InputFilter[] createLengthFilter(InputFilter[] filter, int length) {
    InputFilter[] f = Arrays.copyOf(filter, filter.length + 1);
    f[f.length - 1] = new InputFilter.LengthFilter(length);
    return f;//w w  w.  j  a v a 2 s. c  o  m
}

From source file:Main.java

public static byte[] concat(byte[] first, byte[] second) {
    byte[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
}

From source file:Main.java

/**
 * A generic method for gluing lists of objects.
 *
 * @param first/*from   w ww . ja  v a2  s.  c o m*/
 *            the first array.
 * @param rest
 *            the rest of the arrays.
 * @return
 */
public static <T> T[] arrCat(T[] first, T[]... rest) {
    int ttlLen = first.length;
    for (T[] arr : rest) {
        ttlLen += arr.length;
    }
    T[] result = Arrays.copyOf(first, ttlLen);
    int currOfst = first.length;
    for (T[] arr : rest) {
        System.arraycopy(arr, 0, result, currOfst, arr.length);
        currOfst += arr.length;
    }

    return result;
}

From source file:Main.java

/**
 * @param <T>// w w  w  . j  ava 2  s . c o m
 * @param elements
 * @return a mutable copy of {@code elements}.
 */
public static <T> T[] array(T... elements) {
    return Arrays.copyOf(elements, elements.length);
}

From source file:Main.java

static String toBase58(final byte[] in) {

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

    Arrays.fill(indexes, -1);/*  w ww. j  a v a2  s .  c  o m*/

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

    for (int k = 0; k < ab.length; ++k)
        indexes[ab[k]] = k;

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

From source file:Main.java

/**
 * Concatenates a list of int arrays into a single array.
 * //from  w  w  w .  j a  va  2  s . c om
 * @param arrays The arrays.
 * @return The concatenated array.
 * 
 * @see {@link http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java}
 */
public static int[] concatAllInt(int[]... arrays) {
    int totalLength = 0;
    final int subArrayCount = arrays.length;
    for (int i = 0; i < subArrayCount; ++i) {
        totalLength += arrays[i].length;
    }
    int[] result = Arrays.copyOf(arrays[0], totalLength);
    int offset = arrays[0].length;
    for (int i = 1; i < subArrayCount; ++i) {
        System.arraycopy(arrays[i], 0, result, offset, arrays[i].length);
        offset += arrays[i].length;
    }
    return result;
}