Java Array Range Copy copyOf(T[] oriArray, int newArraySize, int startOffset)

Here you can find the source of copyOf(T[] oriArray, int newArraySize, int startOffset)

Description

Generates an copy of the array with some size modification.

License

Open Source License

Parameter

Parameter Description
T a parameter
oriArray a parameter
newArraySize a parameter
startOffset a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T> T[] copyOf(T[] oriArray, int newArraySize, int startOffset) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  w  w w  .  ja  v  a 2  s.  co  m*/
     * Generates an copy of the array with some size modification.
     * TODOC
     *
     * @param <T>
     * @param oriArray
     * @param newArraySize
     * @param startOffset
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] copyOf(T[] oriArray, int newArraySize, int startOffset) {
        // In Java 6 this would be a simple: Arrays.copyOf(msgArgs, argCount+1);
        // But the code should stay compatible to Java 5 for now.
        Object[] targetArray = new Object[newArraySize];

        if (oriArray != null) {
            for (int i = startOffset, count = 0; i < newArraySize && count < oriArray.length; ++i, ++count) {
                targetArray[i] = oriArray[count];
            }
        }

        return (T[]) targetArray;
    }

    public static <T> T[] copyOf(T[] oriArray, int newArraySize) {
        return copyOf(oriArray, newArraySize, 0);
    }
}

Related

  1. copyOf(Object src, int newLength)
  2. copyOf(Object[] array, int newLength)
  3. copyOf(Object[] values, int nlen)
  4. copyOf(String[] data, int newLength)
  5. copyOf(String[] original, int newLength)
  6. copyOfArray(int[] a)
  7. copyOfRange(byte[] bytes, int offset, int len)
  8. copyOfRange(byte[] original, int from, int to)
  9. copyOfRange(byte[] original, int from, int to)