List of usage examples for java.lang System arraycopy
@HotSpotIntrinsicCandidate public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
From source file:Main.java
public static String[] shift(String[] array, int n) { if (n <= 0) { throw new IllegalArgumentException("n must a non-negative number."); }// w ww . j av a 2s . com if (n >= array.length) { return new String[0]; } String[] dest = new String[array.length - n]; System.arraycopy(array, n, dest, 0, array.length - n); return dest; }
From source file:Main.java
public static byte[] concatAll(byte[] first, byte[]... rest) { int i, lenOfReadBytes; int totalLength = lenOfReadBytes = i = first != null ? first.length : 0; for (byte[] array : rest) { if (array != null) totalLength += array.length; }/*from w w w .j a v a2s .c om*/ byte[] result = new byte[totalLength]; if (first != null) { System.arraycopy(first, 0, result, 0, first.length); } int b = 0; for (; i < totalLength; i++) { if (rest == null) b++; if (i >= rest[b].length + lenOfReadBytes) { lenOfReadBytes += rest[b++].length; } result[i] = rest[b][i - lenOfReadBytes]; } return result; }
From source file:Main.java
public static <T extends Object> T[] addItem(T[] array, T item, int index) { int size = array == null ? 0 : array.length; if (index < 0 || index > size) index = size;//from w ww. j av a2 s . c o m @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(item.getClass(), size + 1); if (index > 0 && array != null) System.arraycopy(array, 0, result, 0, index); result[index] = item; if (index < result.length - 1 && array != null) System.arraycopy(array, index, result, index + 1, array.length - index); return result; }
From source file:Main.java
/** * Resizes an array of Objects to a specified size. * * @param list Array to be resized.// w ww . j a v a 2s . c o m * @param newSize Array size after resizing * @return Array of type Object with the specified size */ static public Object resizeArray(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 source file:DoubleArray.java
static int[] doubleArray(int original[]) { int length = original.length; int newArray[] = new int[length * 2]; System.arraycopy(original, 0, newArray, 0, length); return newArray; }
From source file:Main.java
public static boolean[] combine(boolean[]... arrays) { int totalLength = 0; for (boolean[] booleans : arrays) { totalLength += booleans.length;//from w ww . j a v a2 s . c o m } boolean[] newArray = new boolean[totalLength]; int pointer = 0; for (boolean[] booleans : arrays) { System.arraycopy(booleans, 0, newArray, pointer, booleans.length); pointer += booleans.length; } return newArray; }
From source file:Main.java
public static byte[][] chunkArray(byte[] array, int chunkSize) { int numOfChunks = (int) Math.ceil((double) array.length / chunkSize); byte[][] output = new byte[numOfChunks][]; for (int i = 0; i < numOfChunks; ++i) { int start = i * chunkSize; int length = Math.min(array.length - start, chunkSize); byte[] temp = new byte[length]; System.arraycopy(array, start, temp, 0, length); output[i] = temp;//from ww w . j a v a 2 s . c om } return output; }
From source file:Main.java
public static <T> T[] concatenateArrays(T[] array1, T[] array2) { if (isEmpty(array1)) { return array2; }//from w ww. j a va 2 s . c om if (isEmpty(array2)) { return array1; } T[] resArray = (T[]) java.lang.reflect.Array.newInstance(array1[0].getClass(), array1.length + array2.length); System.arraycopy(array1, 0, resArray, 0, array1.length); System.arraycopy(array2, 0, resArray, array1.length, array2.length); return resArray; }
From source file:Main.java
public static <T> T[] joinArrays(final Class<T> type, final T[]... arrs) { int total = 0; for (final T[] arr : arrs) { if (arr != null) total += arr.length;//from ww w.ja va2 s . c o m } final T[] ret = (T[]) Array.newInstance(type, total); int x = 0; for (final T[] arr : arrs) { if (arr != null) { System.arraycopy(arr, 0, ret, x, arr.length); x += arr.length; } } return ret; }
From source file:Main.java
public static byte[] concatenate(byte[]... bytes) { int length = 0; for (byte[] array : bytes) length += array.length;//from w ww. jav a 2s . co m byte[] result = new byte[length]; int offset = 0; for (byte[] array : bytes) { System.arraycopy(array, 0, result, offset, array.length); offset += array.length; } return result; }