Java Array Sub Array subArray(T[] array, int start, int end)

Here you can find the source of subArray(T[] array, int start, int end)

Description

Takes a sub array from element start to element end (end is excluded)

License

Open Source License

Parameter

Parameter Description
array the base array
start start element (included)
end end element (excluded)
T type of the array

Return

the sub array of type T

Declaration

public static <T> T[] subArray(T[] array, int start, int end) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    /**/*from   ww  w.  ja  v a  2  s.c  o  m*/
     * Takes a sub array from element start to element end (end is excluded)
     *
     * @param array the base array
     * @param start start element (included)
     * @param end   end element (excluded)
     * @param <T>   type of the array
     * @return the sub array of type T
     */
    public static <T> T[] subArray(T[] array, int start, int end) {
        return Arrays.copyOfRange(array, start, end);
    }

    /**
     * Takes a sub array from the start with given size
     * It will not go out of bounds so sub array can be smaller than the given size
     *
     * @param array the base array
     * @param size  size of the sub array to take
     * @param <T>   type of the array
     * @return sub array of given type
     */
    public static <T> T[] subArray(T[] array, int size) {
        return subArray(array, 0, size);
    }

    public static int[] subArray(int[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static double[] subArray(double[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static float[] subArray(float[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static byte[] subArray(byte[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static short[] subArray(short[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static long[] subArray(long[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static boolean[] subArray(boolean[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static char[] subArray(char[] array, int start, int end) {
        return toPrimitiveArray(subArray(toObjectArray(array), start, end));
    }

    public static int[] subArray(int[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static double[] subArray(double[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static float[] subArray(float[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static byte[] subArray(byte[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static short[] subArray(short[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static long[] subArray(long[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static boolean[] subArray(boolean[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static char[] subArray(char[] array, int size) {
        return toPrimitiveArray(subArray(toObjectArray(array), size));
    }

    public static int[] toPrimitiveArray(Integer[] array) {
        int[] result = new int[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? 0 : array[i]);
        return result;
    }

    public static double[] toPrimitiveArray(Double[] array) {
        double[] result = new double[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? 0 : array[i]);
        return result;
    }

    public static float[] toPrimitiveArray(Float[] array) {
        float[] result = new float[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? 0 : array[i]);
        return result;
    }

    public static byte[] toPrimitiveArray(Byte[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? 0 : array[i]);
        return result;
    }

    public static short[] toPrimitiveArray(Short[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? 0 : array[i]);
        return result;
    }

    public static long[] toPrimitiveArray(Long[] array) {
        long[] result = new long[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? 0 : array[i]);
        return result;
    }

    public static boolean[] toPrimitiveArray(Boolean[] array) {
        boolean[] result = new boolean[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? false : array[i]);
        return result;
    }

    public static char[] toPrimitiveArray(Character[] array) {
        char[] result = new char[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = (array[i] == null ? ' ' : array[i]);
        return result;
    }

    public static Integer[] toObjectArray(int[] array) {
        Integer[] result = new Integer[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Double[] toObjectArray(double[] array) {
        Double[] result = new Double[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Float[] toObjectArray(float[] array) {
        Float[] result = new Float[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Byte[] toObjectArray(byte[] array) {
        Byte[] result = new Byte[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Short[] toObjectArray(short[] array) {
        Short[] result = new Short[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Long[] toObjectArray(long[] array) {
        Long[] result = new Long[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Boolean[] toObjectArray(boolean[] array) {
        Boolean[] result = new Boolean[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }

    public static Character[] toObjectArray(char[] array) {
        Character[] result = new Character[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] = array[i];
        return result;
    }
}

Related

  1. subArray(String[] data, int index, int length)
  2. subarray(String[] master, int index)
  3. subArray(String[] strings, int nStart)
  4. subarray(T[] a, int off, int len)
  5. subArray(T[] array, int start, int end)
  6. subArray(T[] array, int startIndex, int endIndex)
  7. subArray(T[] origin, int start, int length)
  8. subarrayChar2Double(char[] orig, int off, int len)
  9. subarrayEnd(byte[] array, int offset)