Returns subarray. - Java Collection Framework

Java examples for Collection Framework:Array Sub Array

Description

Returns subarray.

Demo Code

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.
import java.lang.reflect.Array;
import static jodd.util.StringPool.NULL;

public class Main{
    /**//from  w  ww.  j a v a 2 s  . c o  m
     * Returns subarray.
     */
    public static <T> T[] subarray(T[] buffer, int offset, int length) {
        Class<T> componentType = (Class<T>) buffer.getClass()
                .getComponentType();
        return subarray(buffer, offset, length, componentType);
    }
    /**
     * Returns subarray.
     */
    @SuppressWarnings({ "unchecked" })
    public static <T> T[] subarray(T[] buffer, int offset, int length,
            Class<T> componentType) {
        T[] temp = (T[]) Array.newInstance(componentType, length);
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static String[] subarray(String[] buffer, int offset, int length) {
        String temp[] = new String[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static byte[] subarray(byte[] buffer, int offset, int length) {
        byte temp[] = new byte[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static char[] subarray(char[] buffer, int offset, int length) {
        char temp[] = new char[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static short[] subarray(short[] buffer, int offset, int length) {
        short temp[] = new short[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static int[] subarray(int[] buffer, int offset, int length) {
        int temp[] = new int[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static long[] subarray(long[] buffer, int offset, int length) {
        long temp[] = new long[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static float[] subarray(float[] buffer, int offset, int length) {
        float temp[] = new float[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static double[] subarray(double[] buffer, int offset, int length) {
        double temp[] = new double[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
    /**
     * Returns subarray.
     */
    public static boolean[] subarray(boolean[] buffer, int offset,
            int length) {
        boolean temp[] = new boolean[length];
        System.arraycopy(buffer, offset, temp, 0, length);
        return temp;
    }
}

Related Tutorials