集册 Java实例教程 返回子数组。

返回子数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

474
返回子数组。

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.

import java.lang.reflect.Array;

import static jodd.util.StringPool.NULL;

/**
NowJava.com
**/

public class Main{

    /**

     * 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);/*来 自 时 代 J a v a - nowjava.com*/

        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
展开阅读全文