Java Array Sub Array subarray(final long[] array, int startIndexInclusive, int endIndexExclusive)

Here you can find the source of subarray(final long[] array, int startIndexInclusive, int endIndexExclusive)

Description

Produces a new long array containing the elements between the start and end indices.

License

Apache License

Parameter

Parameter Description
array the array
startIndexInclusive the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in an empty array.
endIndexExclusive elements up to endIndex-1 are present in the returned subarray. Undervalue (< startIndex) produces empty array, overvalue (>array.length) is demoted to array length.

Return

a new array containing the elements between the start and end indices.

Declaration

public static long[] subarray(final long[] array, int startIndexInclusive, int endIndexExclusive) 

Method Source Code

//package com.java2s;
/*// w  w w  . j a va2  s.com
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /**
     * An empty immutable {@code long} array.
     */
    public static final long[] EMPTY_LONG_ARRAY = new long[0];
    /**
     * An empty immutable {@code int} array.
     */
    public static final int[] EMPTY_INT_ARRAY = new int[0];
    /**
     * An empty immutable {@code short} array.
     */
    public static final short[] EMPTY_SHORT_ARRAY = new short[0];
    /**
     * An empty immutable {@code byte} array.
     */
    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
    /**
     * An empty immutable {@code double} array.
     */
    public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
    /**
     * An empty immutable {@code float} array.
     */
    public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
    /**
     * An empty immutable {@code boolean} array.
     */
    public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
    /**
     * An empty immutable {@code char} array.
     */
    public static final char[] EMPTY_CHAR_ARRAY = new char[0];

    /**
     * <p>
     * Produces a new {@code long} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(long[], int, int)
     */
    public static long[] subarray(final long[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_LONG_ARRAY;
        }

        final long[] subarray = new long[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code int} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(int[], int, int)
     */
    public static int[] subarray(final int[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_INT_ARRAY;
        }

        final int[] subarray = new int[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code short} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(short[], int, int)
     */
    public static short[] subarray(final short[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_SHORT_ARRAY;
        }

        final short[] subarray = new short[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code char} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(char[], int, int)
     */
    public static char[] subarray(final char[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_CHAR_ARRAY;
        }

        final char[] subarray = new char[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code byte} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(byte[], int, int)
     */
    public static byte[] subarray(final byte[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_BYTE_ARRAY;
        }

        final byte[] subarray = new byte[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code double} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(double[], int, int)
     */
    public static double[] subarray(final double[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_DOUBLE_ARRAY;
        }

        final double[] subarray = new double[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code float} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(float[], int, int)
     */
    public static float[] subarray(final float[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_FLOAT_ARRAY;
        }

        final float[] subarray = new float[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }

    /**
     * <p>
     * Produces a new {@code boolean} array containing the elements between the start and end indices.
     * </p>
     * 
     * <p>
     * The start index is inclusive, the end index exclusive. Null array input produces null output.
     * </p>
     * 
     * @param array the array
     * @param startIndexInclusive the starting index. Undervalue (&lt;0) is promoted to 0, overvalue
     *        (&gt;array.length) results in an empty array.
     * @param endIndexExclusive elements up to endIndex-1 are present in the returned subarray.
     *        Undervalue (&lt; startIndex) produces empty array, overvalue (&gt;array.length) is
     *        demoted to array length.
     * @return a new array containing the elements between the start and end indices.
     * @since 2.1
     * @see Arrays#copyOfRange(boolean[], int, int)
     */
    public static boolean[] subarray(final boolean[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        final int newSize = endIndexExclusive - startIndexInclusive;
        if (newSize <= 0) {
            return EMPTY_BOOLEAN_ARRAY;
        }

        final boolean[] subarray = new boolean[newSize];
        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
        return subarray;
    }
}

Related

  1. subArray(final byte[] source, final int start, final int length)
  2. subArray(final byte[] src, final int srcPos, final int length)
  3. subArray(final char[] source, int from, int to)
  4. subArray(final int[] input, final int start, final int end)
  5. subArray(final long[] array, final int start, final int end)
  6. subarray(final String[] arr, int first, final int last)
  7. subArray(float[] array, int start)
  8. subarray(int[] array, int offset, int length)
  9. subarray(int[] array, int start, int end)