Example usage for java.lang System arraycopy

List of usage examples for java.lang System arraycopy

Introduction

In this page you can find the example usage for java.lang System arraycopy.

Prototype

@HotSpotIntrinsicCandidate
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Source Link

Document

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Usage

From source file:Main.java

/**
 * Append one or more elements to an array (Add them to the end).
 *
 * @param arr          The array the elements will be appended to.
 * @param lastElements The elements to append, in order (first to last).
 * @return The array with all elements (The same as the {@code arr} argument).
 *//*w  w w  .j  av a 2  s.  com*/
@SafeVarargs
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] arr, T... lastElements) {
    T[] ret = (T[]) new Object[arr.length + lastElements.length];
    System.arraycopy(lastElements, 0, ret, arr.length, lastElements.length);
    System.arraycopy(arr, 0, ret, 0, arr.length);
    return ret;
}

From source file:Main.java

public static void mergeArray(final Object[] dest, final Object[]... arrays) {
    if (arrays == null || arrays.length == 0)
        return;// ww w  .  ja v a2s. c  o  m
    if (arrays.length == 1) {
        final Object[] array = arrays[0];
        System.arraycopy(array, 0, dest, 0, array.length);
        return;
    }
    for (int i = 0, j = arrays.length - 1; i < j; i++) {
        final Object[] array1 = arrays[i], array2 = arrays[i + 1];
        System.arraycopy(array1, 0, dest, 0, array1.length);
        System.arraycopy(array2, 0, dest, array1.length, array2.length);
    }
}

From source file:Main.java

public static byte[] concat(byte[] arr1, int offset1, int len1, byte[] arr2, int offset2, int len2) {
    if (len1 == 0) {
        return subarray(arr2, offset2, len2);
    } else if (len2 == 0) {
        return subarray(arr1, offset1, len1);
    } else {/*from ww  w . j  a v a 2s. c  o m*/
        byte[] result = new byte[len1 + len2];
        System.arraycopy(arr1, offset1, result, 0, len1);
        System.arraycopy(arr2, offset2, result, len1, len2);
        return result;
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[][] partition(T[] bigArray, int max) {
    int nItems = bigArray.length;
    int nPartArrays = (nItems + max - 1) / max;

    T[][] partArrays = (T[][]) Array.newInstance(bigArray.getClass().getComponentType(), nPartArrays, 0);

    // nItems is now the number of remaining items
    for (int i = 0; nItems > 0; i++) {
        int n = (nItems < max) ? nItems : max;
        partArrays[i] = (T[]) Array.newInstance(bigArray.getClass().getComponentType(), n);
        System.arraycopy(bigArray, i * max, partArrays[i], 0, n);

        nItems -= n;/*from   www . jav  a2  s .c  om*/
    }

    return partArrays;
}

From source file:Array.java

public static Object[] insertArrayObject(Object[] vals, Object val, int idx) {
    Object[] newVals = new Object[vals.length + 1];
    if (idx > 0)
        System.arraycopy(vals, 0, newVals, 0, idx);
    newVals[idx] = val;
    if (idx < vals.length)
        System.arraycopy(vals, idx, newVals, idx + 1, vals.length - idx);
    return newVals;
}

From source file:Main.java

public static int[] subArray(final int[] array, final int start, final int end) {
    final int length = end - start;
    if (length < 0)
        throw new IllegalArgumentException();
    final int[] result = new int[length];
    System.arraycopy(array, start, result, 0, length);
    return result;
}

From source file:Main.java

/**
 * Inserts one array into another by replacing specified offset.
 */// w ww .j a v  a2  s. c o m
public static String[] insertAt(String[] dest, String[] src, int offset) {
    String[] temp = new String[dest.length + src.length - 1];
    System.arraycopy(dest, 0, temp, 0, offset);
    System.arraycopy(src, 0, temp, offset, src.length);
    System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
    return temp;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static int[] arrayCopyOf(int[] array, int length) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Arrays.copyOf(array, length);
    } else {/* w ww.  ja v a 2 s.  c o  m*/
        int[] newArray = new int[length];
        System.arraycopy(array, 0, newArray, 0, length);
        return newArray;
    }
}

From source file:Main.java

public static long[] duplicateArray(long[] array) {
    long[] copy = new long[array.length];
    System.arraycopy(array, 0, copy, 0, array.length);
    return copy;/*from ww w.j  a v  a 2s  .  co m*/
}

From source file:Main.java

/**
 * Convenience function for concatenating two arrays
 * @param <T>// www  .  j  a  v a  2 s.  c o  m
 * @param first
 * @param second
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] concat(T[] first, T[] second) {
    T[] result = (T[]) new Object[first.length + second.length];
    System.arraycopy(first, 0, result, 0, first.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
}