Java Utililty Methods Array Range Copy

List of utility methods to do Array Range Copy

Description

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

Method

byte[]copyOfRange(final byte[] original, final int from, final int to)
copy Of Range
final int newLength = to - from;
if (newLength < 0) {
    throw new IllegalArgumentException(from + " > " + to);
final byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
return copy;
double[]copyOfRange(final double[] data, final int start, final int end)
Behavior is identical to calling data.clone() or Arrays.copyOf(data) But can be up to 30% faster if the JIT doesn't optimize those functions
if (end > data.length || start < 0)
    throw new IllegalArgumentException("Bad array bounds!");
final double ret[] = new double[end - start];
for (int i = 0; i < (end - start) / 3; i++) {
    final int x = i * 3;
    ret[x] = data[x + start];
    ret[x + 1] = data[x + 1 + start];
    ret[x + 2] = data[x + 2 + start];
...
int[]copyOfRange(int[] anArray, int from, int to)
Returns a copy of given range of given array (this method is in Java 6 Arrays class).
int newLength = to - from;
if (newLength < 0)
    throw new IllegalArgumentException(from + " > " + to);
int[] copy = new int[newLength];
System.arraycopy(anArray, from, copy, 0, Math.min(anArray.length - from, newLength));
return copy;
int[]copyOfRange(int[] old, int from, int to)
Identical to Arrays.copyOfRange , but GWT compatible.
int length = to - from;
int[] newArray = new int[length];
int minLength = Math.min(old.length - from, length);
System.arraycopy(old, from, newArray, 0, minLength);
return newArray;
int[]copyOfRange(int[] original, int from, int to)
Copied from class Arrays#copyOfRange(int[],int,int) , as this requires java 1.6, and our app should work with 1.5.
int newLength = to - from;
if (newLength < 0)
    throw new IllegalArgumentException(from + " > " + to);
int[] copy = new int[newLength];
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
return copy;
String[]copyOfRange(String[] array, int initialIndex, int endIndex)
copy Of Range
String[] tempArray = new String[endIndex - initialIndex];
for (int i = initialIndex; i < endIndex; i++)
    tempArray[i - initialIndex] = array[i];
return tempArray;
String[]copyOfRange(String[] original, int from, int newLength)
copy Of Range
String[] copy = new String[newLength];
newLength = Math.min(original.length - from, newLength);
System.arraycopy(original, from, copy, 0, newLength);
return copy;