Java Array Slice slice(T[] array, int index, int length)

Here you can find the source of slice(T[] array, int index, int length)

Description

Returns a new array which is a subset of elements from the given array.

License

Open Source License

Parameter

Parameter Description
array The array to be sliced.
index The zero-based start index of the subset; supports ruby-style reverse indexing where, for example, -1 is the last item and -2 is the second last item in the array.
length The desired length of the slice; supports negative lengths for slicing backwards from the end of the array.
T The class of the items stored in the array.

Return

A new array which is a subset of the given array taken at the desired index for the desired length.

Declaration

public static <T> T[] slice(T[] array, int index, int length) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .j ava  2  s  .c  o m*/
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.util.Arrays;

public class Main {
    /**
     * Returns a new array which is a subset of elements from the given array.
     *
     * @param array  The array to be sliced.
     * @param index  The zero-based start index of the subset; supports ruby-style reverse indexing where, for example,
     *               -1 is the last item and -2 is the second last item in the array.
     * @param length The desired length of the slice; supports negative lengths for slicing backwards from the end of
     *               the array.
     * @param <T>    The class of the items stored in the array.
     * @return A new array which is a subset of the given array taken at the desired index for the desired length.
     */
    public static <T> T[] slice(T[] array, int index, int length) {
        if (array == null || array.length == 0)
            return array;
        // support reverse/tail length
        if (length < 0)
            length = array.length + length;
        // support reverse/tail indexing
        if (index < 0)
            index += array.length;
        // don't slice past the end of the array
        if ((length += index) > array.length)
            length = array.length;

        return Arrays.copyOfRange(array, index, length);
    }
}

Related

  1. slice(String[] array, int index, int length)
  2. slice(String[] o, int index)
  3. slice(String[] strings, int begin, int length)
  4. slice(String[] strings, int begin, int length)
  5. slice(T[] array, int index)
  6. slice(T[] array, int start, int finish)
  7. slice(T[] items, int offset, int length)
  8. sliceArray(final String[] array, final int start)
  9. SliceByteArray(byte data[], int offset, int length)