Android Array Range Copy copyOfRange(T[] original, int from, int to)

Here you can find the source of copyOfRange(T[] original, int from, int to)

Description

copy Of Range

Declaration

@SuppressWarnings("unchecked")
    public static <T> T[] copyOfRange(T[] original, int from, int to) 

Method Source Code

import java.lang.reflect.Array;

public class Main{
    @SuppressWarnings("unchecked")
    public static <T> T[] copyOfRange(T[] original, int from, int to) {
        return Arrays.copyOfRange(original, from, to,
                (Class<T[]>) original.getClass());
    }//from  w  w w .  j  av a  2 s .  co m
    @SuppressWarnings("unchecked")
    public static <T, U> T[] copyOfRange(U[] original, int from, int to,
            Class<? extends T[]> newType) {
        int newSize = to - from;
        if (newSize < 0) {
            throw new IllegalArgumentException(from + " > " + to);
        }
        T[] copy = (Object) newType == (Object) Object[].class ? (T[]) new Object[newSize]
                : (T[]) Array.newInstance(newType.getComponentType(),
                        newSize);
        System.arraycopy(original, from, copy, 0,
                Math.min(original.length - from, newSize));
        return copy;
    }
}

Related

  1. copyOfRange(T[] original, int start, int end)
  2. copyOfRange(U[] original, int from, int to, Class newType)
  3. copyOfRange(byte[] original, int from, int to)
  4. copyOfRange(final byte[] source, final int from, final int to)