Java Array Range Copy copyOfRange(final double[] data, final int start, final int end)

Here you can find the source of copyOfRange(final double[] data, final int start, final int end)

Description

Behavior is identical to calling data.clone() or Arrays.copyOf(data) But can be up to 30% faster if the JIT doesn't optimize those functions

License

Open Source License

Parameter

Parameter Description
data a parameter

Declaration

public static final double[] copyOfRange(final double[] data, final int start, final int end) 

Method Source Code

//package com.java2s;
/*// w ww.jav  a  2  s  .  c o  m
 * ====================================================
 * Copyright (C) 2013 by Idylwood Technologies, LLC. All rights reserved.
 *
 * Developed at Idylwood Technologies, LLC.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The License should have been distributed to you with the source tree.
 * If not, it can be found 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.
 *
 * Author: Charles Cooper
 * Date: 2013
 * ====================================================
 */

public class Main {
    /**
     * Behavior is identical to calling data.clone() or Arrays.copyOf(data)
     * But can be up to 30% faster if the JIT doesn't optimize those functions
     * @param data
     * @return
     */
    public static final double[] copyOfRange(final double[] data, final int start, final int end) {
        if (end > data.length || start < 0)
            throw new IllegalArgumentException("Bad array bounds!");
        final double ret[] = new double[end - start];
        for (int i = 0; i < (end - start) / 3; i++) {
            final int x = i * 3;
            ret[x] = data[x + start];
            ret[x + 1] = data[x + 1 + start];
            ret[x + 2] = data[x + 2 + start];
        }
        // Don't care about extra copies if data.length%3==0
        ret[ret.length - 2] = data[end - 2];
        ret[ret.length - 1] = data[end - 1];
        return ret;
    }
}

Related

  1. copyOfRange(byte[] original, int from, int to)
  2. copyOfRange(byte[] original, int from, int to)
  3. copyOfRange(char[] original, int from, int to)
  4. copyOfRange(final byte[] array, final int startIndex, final int endIndex)
  5. copyOfRange(final byte[] original, final int from, final int to)
  6. copyOfRange(int[] anArray, int from, int to)
  7. copyOfRange(int[] old, int from, int to)
  8. copyOfRange(int[] original, int from, int to)
  9. copyOfRange(String[] array, int initialIndex, int endIndex)