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

Object[]copyArray(Object[] array, int from, int to)
(Partial) replacement for Arrays.copyOfRange, which is only available in JDK6.
Object[] result = new Object[to - from];
System.arraycopy(array, from, result, 0, to - from);
return result;
voidcopyArray(Object[] from, Object[] to)
copy Array
for (int i = 0; i < from.length; i++) {
    to[i] = from[i];
String[]copyArray(String[] array)
copy Array
if (array.length == 0) {
    return EMPTY_STRING;
String[] copy = new String[array.length];
System.arraycopy(array, 0, copy, 0, array.length);
return copy;
String[]copyArray(String[] originalArray)
Copy a string array.
String[] copyOf = new String[originalArray.length];
System.arraycopy(originalArray, 0, copyOf, 0, originalArray.length);
return copyOf;
T[]copyArray(T[] array)
Copies the given array.
if (array != null) {
    return Arrays.copyOf(array, array.length);
return null;
T[]copyArray(T[] original)
Copy an array.
return Arrays.copyOf(original, original.length);
T[]copyArray2(Object v, int len)
copy Array
return Arrays.copyOf((T[]) v, len);
String[]copyArrayAddFirst(String[] arr, String add)
Utility function that copies a string array and add another string to first
String[] arrCopy = new String[arr.length + 1];
arrCopy[0] = add;
System.arraycopy(arr, 0, arrCopy, 1, arr.length);
return arrCopy;
String[]copyArrayCutFirst(String[] arr)
Utility function that copies a string array except for the first element
if (arr.length > 1) {
    String[] arrCopy = new String[arr.length - 1];
    System.arraycopy(arr, 1, arrCopy, 0, arrCopy.length);
    return arrCopy;
} else {
    return new String[0];
String[]copyArrayExceptLast(String[] array)
used when finding elements in path for puts, need the array minus the last one the last is removed to use as the key
String[] smaller = new String[array.length - 1];
for (int i = 0; i < smaller.length; ++i) {
    smaller[i] = array[i];
return smaller;