Java Utililty Methods Array Copy

List of utility methods to do Array Copy

Description

The list of methods to do Array Copy are organized into topic(s).

Method

int[]copyArrayMax(int[] local, int[] merged)
copy int array values and keeps only max in the new one
if (local == null || local.length == 0) {
    local = new int[merged.length];
} else {
    int[] dest = new int[local.length];
    System.arraycopy(local, 0, dest, 0, local.length);
    local = dest;
for (int i = 0; i < merged.length; i++) {
...
byte[]copyArrayRange(final byte[] value, int start, int end)
Copy a sub range of the given array start is included, end is excluded
int noBytes = end - start;
byte[] result = new byte[noBytes];
System.arraycopy(value, start, result, 0, noBytes);
return result;
voidcopyArrays(byte[] dest, byte[] source, int fromIdx)
Describe copyArrays method here.
for (int i = 0; i < dest.length; i++) {
    dest[i] = source[i + fromIdx];
voidcopyArrays(byte[] src, int srcOffset, byte[] dst, int dstOffset, int numBytes)
copy Arrays
assert src != null;
assert srcOffset >= 0;
assert dst != null;
assert dstOffset >= 0;
assert numBytes >= 0;
assert srcOffset + numBytes <= src.length;
assert dstOffset + numBytes <= dst.length;
for (int i = 0; i < numBytes; i++) {
...
StringcopyArrayToStr(String[] src, String del)
copy Array To Str
StringBuilder sb = new StringBuilder();
for (String s : src)
    sb.append(s + del);
String toret = sb.toString();
toret = toret.substring(0, toret.length() - del.length()) + "\n";
src = null;
return toret;
byte[]copyArrayWhenNotNull(byte[] source)
Copies array or returns null when source is null.
if (source != null) {
    return Arrays.copyOf(source, source.length);
} else {
    return null;
byte[]copyBytes(byte[] data, int from, int to)
To copy the range of the data bytes from the data.
return Arrays.copyOfRange(data, from, to);
ListcopyExcept(E[] orig, E excludedElem)
Creates a List copy of orig, with all elements except elements equal to excludedElem.
List<E> rejectedElements = new ArrayList<E>(orig.length);
for (int i = 0; i < orig.length; i++) {
    if (!orig[i].equals(excludedElem)) {
        rejectedElements.add(orig[i]);
return rejectedElements;
int[]copyIfExceeded(int[] arr, int len)
Copies array only if array length greater than needed length.
assert arr != null;
assert 0 <= len && len <= arr.length;
return len == arr.length ? arr : Arrays.copyOf(arr, len);
int[]copyIntoBigger(int[] array, int newSize, int defaultValue)
copy Into Bigger
assert newSize > array.length;
int[] bigger = Arrays.copyOf(array, newSize);
Arrays.fill(bigger, array.length, newSize, defaultValue);
return bigger;