Java Array Range Copy copyOfRange(int[] original, int from, int to)

Here you can find the source of copyOfRange(int[] original, int from, int to)

Description

Copied from class Arrays#copyOfRange(int[],int,int) , as this requires java 1.6, and our app should work with 1.5.

License

Open Source License

Parameter

Parameter Description
original a parameter
from a parameter
to a parameter

Declaration

public static int[] copyOfRange(int[] original, int from, int to) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) Nov 2, 2012 NetXForge./*from w w  w  . jav a  2s . co  m*/
 * 
 * This program 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 3 of the License, or (at your option) any later
 * version.
 * 
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>
 * 
 * Contributors: Christophe Bouhier - initial API and implementation and/or
 * initial documentation
 *******************************************************************************/

public class Main {
    /**
     * Copied from class {@link Arrays#copyOfRange(int[], int, int)} , as this
     * requires java 1.6, and our app should work with 1.5.
     * 
     * @param original
     * @param from
     * @param to
     * @return
     */
    public static int[] copyOfRange(int[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        int[] copy = new int[newLength];
        System.arraycopy(original, from, copy, 0,
                Math.min(original.length - from, newLength));
        return copy;
    }
}

Related

  1. copyOfRange(final byte[] array, final int startIndex, final int endIndex)
  2. copyOfRange(final byte[] original, final int from, final int to)
  3. copyOfRange(final double[] data, final int start, final int end)
  4. copyOfRange(int[] anArray, int from, int to)
  5. copyOfRange(int[] old, int from, int to)
  6. copyOfRange(String[] array, int initialIndex, int endIndex)
  7. copyOfRange(String[] original, int from, int newLength)