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

/**
 * Replace the value of a field containing a non null array, by a new array containing the elements of the original
 * array plus the elements of extraElements.
 *
 * @param instance the instance whose field is to be modified.
 * @param fieldName the field to modify.
 * @param extraElements elements to append at the end of the array.
 *//*from   www  .ja v a 2s.  com*/
static void expandFieldArray(Object instance, String fieldName, Object[] extraElements)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field jlrField = findField(instance, fieldName);
    Object[] original = (Object[]) jlrField.get(instance);
    Object[] combined = (Object[]) Array.newInstance(original.getClass().getComponentType(),
            original.length + extraElements.length);
    System.arraycopy(extraElements, 0, combined, 0, extraElements.length);
    System.arraycopy(original, 0, combined, extraElements.length, original.length);
    jlrField.set(instance, combined);
}

From source file:Main.java

public static Object expand(Object obj, int i, boolean flag) {
    int j = Array.getLength(obj);
    Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j + i);
    System.arraycopy(obj, 0, obj1, flag ? 0 : i, j);
    return obj1;/*from  w  w  w.  j a va 2 s .  c om*/
}

From source file:Main.java

static public Object expandArray(Object[] list, int newSize) {
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
    return temp;//from   w w w  . j a  va 2 s.  c  o m
}

From source file:Main.java

public static double[] copy(double[] dataToCopy) {
    if (dataToCopy == null) {
        return null;
    }/*from w w  w .  ja  v  a  2s .c  o  m*/

    double[] ret = new double[dataToCopy.length];
    System.arraycopy(dataToCopy, 0, ret, 0, dataToCopy.length);

    return ret;
}

From source file:Main.java

public static int[] removeInt(int[] cur, int val) {
    if (cur == null) {
        return null;
    }/*from  w w  w .  j av  a2s  .c  o  m*/
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (cur[i] == val) {
            int[] ret = new int[N - 1];
            if (i > 0) {
                System.arraycopy(cur, 0, ret, 0, i);
            }
            if (i < (N - 1)) {
                System.arraycopy(cur, i + 1, ret, i, N - i - 1);
            }
            return ret;
        }
    }
    return cur;
}

From source file:Main.java

public static byte[] decodeQ(byte[] bytes) throws IOException {
    int len = bytes.length;
    int length = 0;
    for (int i = 0; i < len; i++) {
        byte b = bytes[i];
        if (b == '=') {
            i++;/*from  ww w.  j av a  2s  .c o  m*/
            if (i == len)
                break;
            b = bytes[i];
            if (b == '\r' || b == '\n') {
                b = bytes[++i];
                if (b != '\n') {
                    i--;
                }
                continue;
            }
            int result = -Character.digit(b, 16);
            result *= 16;
            result -= Character.digit(bytes[++i], 16);
            bytes[length++] = (byte) -result;
        } else {
            bytes[length++] = b;
        }
    }
    byte[] result = new byte[length];
    System.arraycopy(bytes, 0, result, 0, length);
    return result;
}

From source file:Main.java

public static long[] addAll(long[] array1, long[] array2) {
    if (array1 == null) {
        return clone(array2);
    } else if (array2 == null) {
        return clone(array1);
    }//from   w ww.  j a va  2 s.  co  m
    long[] joinedArray = new long[array1.length + array2.length];
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <T> T[] remove(T[] array, T obj) {
    int index = indexOf(array, obj);
    if (index < 0) {
        return array;
    }/*from   ww  w  .jav  a  2s . com*/
    T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length - 1);
    if (index > 0) {
        System.arraycopy(array, 0, newArray, 0, index);
    }
    if (index < array.length - 1) {
        System.arraycopy(array, index + 1, newArray, index, newArray.length - index);
    }
    return newArray;
}

From source file:Main.java

public static int[] appendInt(int[] cur, int val) {
    if (cur == null) {
        return new int[] { val };
    }//from   www . ja  v  a2  s . com
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (cur[i] == val) {
            return cur;
        }
    }
    int[] ret = new int[N + 1];
    System.arraycopy(cur, 0, ret, 0, N);
    ret[N] = val;
    return ret;
}

From source file:Main.java

public static int[] removeInt(final int[] cur, final int val) {
    if (cur == null) {
        return null;
    }/* ww  w .j ava2 s  . c  om*/
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (cur[i] == val) {
            final int[] ret = new int[N - 1];
            if (i > 0) {
                System.arraycopy(cur, 0, ret, 0, i);
            }
            if (i < (N - 1)) {
                System.arraycopy(cur, i + 1, ret, i, N - i - 1);
            }
            return ret;
        }
    }
    return cur;
}