Java Array Sub Array subArray(Object[] arr, int start, int length)

Here you can find the source of subArray(Object[] arr, int start, int length)

Description

Creates a subarray of arr starting at start and continuing numerically until start + length has been reached.

License

Open Source License

Parameter

Parameter Description
arr The array to be transformed.
start The starting index.
length The number of elements desired.

Exception

Parameter Description
ArrayIndexOutOfBoundsException an exception

Return

{arr[start], ..., arr[start + length]}

Declaration

public static Object[] subArray(Object[] arr, int start, int length) 

Method Source Code

//package com.java2s;
/* This file is part of Math4J.
 * Math4J is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version./*  w  w w  .  j av  a  2 s  .c  om*/
 *
 * Math4J is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Math4J; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * Copyright 2005 Anthony Magee
 */

public class Main {
    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static Object[] subArray(Object[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            Object[] reply = new Object[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static boolean[] subArray(boolean[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            boolean[] reply = new boolean[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static byte[] subArray(byte[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            byte[] reply = new byte[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static char[] subArray(char[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            char[] reply = new char[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static double[] subArray(double[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            double[] reply = new double[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static float[] subArray(float[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            float[] reply = new float[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static int[] subArray(int[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            int[] reply = new int[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static long[] subArray(long[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            long[] reply = new long[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }

    /**
     * Creates a subarray of <code>arr</code> starting at <code>start</code>
     * and continuing numerically until <code>start</code> +
     * <code>length</code> has been reached. If <code>arr</code> does not
     * contain enough elements to satisfy <code>length</code> the returned
     * array will have null elements in the unfilled indices.
     * 
     * @param arr The array to be transformed.
     * @param start The starting index.
     * @param length The number of elements desired.
     * @return {arr[start], ..., arr[start + length]}
     * @throws ArrayIndexOutOfBoundsException
     */
    public static short[] subArray(short[] arr, int start, int length) {
        if (start < arr.length) {
            if (arr.length - start < length)
                length = arr.length - start;
            short[] reply = new short[length];
            for (int i = 0; i < length; i++) {
                try {
                    reply[i] = arr[i + start];
                } catch (ArrayIndexOutOfBoundsException e) {
                }
            }
            return reply;
        } else
            throw new ArrayIndexOutOfBoundsException(start + " is not a valid start index.");
    }
}

Related

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