Java Array Sub Array subArray(byte[] a, int beginIndex, int endIndex)

Here you can find the source of subArray(byte[] a, int beginIndex, int endIndex)

Description

Returns the new array that is the sub-array of the specified array .

License

Open Source License

Parameter

Parameter Description
a array data to be copied.
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.

Return

sub-array of the a. If parameters are not correct, will return new byte[0].

Declaration

public static byte[] subArray(byte[] a, int beginIndex, int endIndex) 

Method Source Code

//package com.java2s;
/*//  w ww  .j  av  a 2  s . c  o  m
* Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*
*/

public class Main {
    /**
     * Returns the new array that is the sub-array of the specified array .
     * The returned array begins at the specified beginIndex and extends to
     * the byte at index endIndex - 1. Thus the length of the new array is
     * endIndex-beginIndex. If <code>a</code> is <code>null</code> or empty,
     * this function returns <code>null</code>. Also returns <code>null</code>
     * if the beginIndex is negative, or endIndex is larger than the length of
     * the <code>a</code> array, or beginIndex is larger than endIndex.
     *
     * @param a  array data to be copied.
     * @param beginIndex - the beginning index, inclusive.
     * @param endIndex - the ending index, exclusive.
     * @return sub-array of the <code>a</code>. If parameters are not correct,
     * will return new byte[0].
     */
    public static byte[] subArray(byte[] a, int beginIndex, int endIndex) {
        if (a == null || a.length == 0 || beginIndex < 0 || endIndex > a.length || beginIndex > endIndex) {
            return new byte[0];
        } else {
            byte[] b = new byte[endIndex - beginIndex];
            System.arraycopy(a, beginIndex, b, 0, b.length);
            return b;
        }
    }
}

Related

  1. sub(String[] a, String[] b)
  2. sub(T[] source, int first, int last)
  3. subarray(boolean[] array, int fromIndex, int length)
  4. subArray(byte[] array, int beginIndex, int endIndex)
  5. subarray(byte[] array, int offset, int length)
  6. subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)
  7. subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)