/** * Trims the given array to the given length. * * @param array * an array. * @param length * the new maximum length for the array. * @return {@code array}, if it contains {@code length} entries or less; * otherwise, an array with {@code length} entries whose entries are the * same as the first {@code length} entries of {@code array}. * */ public static <K> K[] trim(final K[] array, final int length) { if (length >= array.length) return array; final K t[] = newArray(array, length); System.arraycopy(array, 0, t, 0, length); return t; } /**
/** * Forces an array to contain the given number of entries, preserving just a * part of the array. * * @param array * an array. * @param length * the new minimum length for this array. * @param preserve * the number of elements of the array that must be preserved in case * a new allocation is necessary. * @return an array with {@code length} entries whose first {@code preserve} * entries are the same as those of {@code array}. */ public static <K> K[] forceCapacity(final K[] array, final int length, final int preserve) { final K t[] = newArray(array, length); System.arraycopy(array, 0, t, 0, preserve); return t; } /**
final int newLength = (int) Math .max(Math.min((long) array.length + (array.length >> 1), Arrays.MAX_ARRAY_SIZE), length); final K t[] = newArray(array, newLength); System.arraycopy(array, 0, t, 0, preserve); return t;
/** * Returns a copy of a portion of an array. * * @param array * an array. * @param offset * the first element to copy. * @param length * the number of elements to copy. * @return a new array containing {@code length} elements of {@code array} * starting at {@code offset}. */ public static <K> K[] copy(final K[] array, final int offset, final int length) { ensureOffsetLength(array, offset, length); final K[] a = newArray(array, length); System.arraycopy(array, offset, a, 0, length); return a; } /**