Java Array Sub Array subArray(Object[] data, int startIndex, int endIndex)

Here you can find the source of subArray(Object[] data, int startIndex, int endIndex)

Description

Get a part on an array

License

Open Source License

Parameter

Parameter Description
data The array to get the sub array from
startIndex The first index of the subarray
endIndex The first index not in the subarray (last index +1)

Return

an array with the elements from data in the fields startIndex to endIndex-1

Declaration

public static Object[] subArray(Object[] data, int startIndex, int endIndex) 

Method Source Code

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

public class Main {
    /**//  w w  w .j  a va 2 s .  c o m
     * Get a part on an array
     * @param data The array to get the sub array from
     * @param startIndex The first index of the subarray
     * @param endIndex The first index not in the subarray (last index +1)
     * @return an array with the elements from data in the fields startIndex to endIndex-1
     */
    public static Object[] subArray(Object[] data, int startIndex, int endIndex) {
        Object[] output = new Object[endIndex - startIndex];
        for (int i = startIndex; i < endIndex; i++) {
            output[i - startIndex] = data[i];
        }
        return output;
    }

    /**
     * Get a part on an array
     * @param data The array to get the sub array from
     * @param startIndex The first index of the subarray
     * @param endIndex The first index not in the subarray (last index +1)
     * @return an array with the elements from data in the fields startIndex to endIndex-1
     */
    public static boolean[] subArray(boolean[] data, int startIndex, int endIndex) {
        boolean[] output = new boolean[endIndex - startIndex];
        for (int i = startIndex; i < endIndex; i++) {
            output[i - startIndex] = data[i];
        }
        return output;
    }

    /**
     * Get a part on an array
     * @param data The array to get the sub array from
     * @param startIndex The first index of the subarray
     * @param endIndex The first index not in the subarray (last index +1)
     * @return an array with the elements from data in the fields startIndex to endIndex-1
     */
    public static double[] subArray(double[] data, int startIndex, int endIndex) {
        double[] output = new double[endIndex - startIndex];
        for (int i = startIndex; i < endIndex; i++) {
            output[i - startIndex] = data[i];
        }
        return output;
    }
}

Related

  1. subarray(int[] array, int start, int end)
  2. subArray(int[] array, int startIndex, int theLength)
  3. subArray(Object a[], int from)
  4. subArray(Object in[], int start, int end)
  5. subArray(Object[] arr, int start, int length)
  6. subArray(String a[], int from)
  7. subarray(String[] args, String sep)
  8. subArray(String[] array, int start, int endExclusive)
  9. subArray(String[] data, int index, int length)