Java Array Copy arrayCopy(T[] objs)

Here you can find the source of arrayCopy(T[] objs)

Description

Copy the given array.

License

Open Source License

Parameter

Parameter Description
T the generic type of the array.
objs the array.

Return

a copy of the array, null if the array is null.

Declaration

public static <T> T[] arrayCopy(T[] objs) 

Method Source Code

//package com.java2s;
/** Various utility functions for Java.
 * <p>/*from w  ww  . ja v  a2 s  .c o  m*/
 * Copyright (c) 2008 Eric Eaton
 * <p>
 * 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.
 * <p>
 * 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.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 * 
 * @author Eric Eaton (EricEaton@umbc.edu) <br>
 *         University of Maryland Baltimore County
 * 
 * @version 0.1
 *
 */

public class Main {
    /** Copy the given array.
     * @param <T> the generic type of the array.
     * @param objs the array.
     * @return a copy of the array, <code>null</code> if the array is null.
     */
    public static <T> T[] arrayCopy(T[] objs) {
        if (objs == null)
            return null;
        Object[] copy = new Object[objs.length];
        System.arraycopy(objs, 0, copy, 0, objs.length);
        return (T[]) copy;
    }

    /** Copy the given array.
     * @param objs the array.
     * @return a copy of the array, <code>null</code> if the array is null.
     */
    public static int[] arrayCopy(int[] objs) {
        if (objs == null)
            return null;
        int[] copy = new int[objs.length];
        System.arraycopy(objs, 0, copy, 0, objs.length);
        return copy;
    }

    /** Copy the given array.
     * @param objs the array.
     * @return a copy of the array, <code>null</code> if the array is null.
     */
    public static double[] arrayCopy(double[] objs) {
        if (objs == null)
            return null;
        double[] copy = new double[objs.length];
        System.arraycopy(objs, 0, copy, 0, objs.length);
        return copy;
    }
}

Related

  1. arrayCopy(Object[] source, Object[] target, int size)
  2. arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int length)
  3. arraycopy(String[] src)
  4. arrayCopy(String[][] src, int src_position, String[][] dst, int dst_position, int length)
  5. arraycopy(T[] a)
  6. arraycopy(T[] src, int srcPos, T[] dest, int destPos, int length)
  7. arraycopy(T[] src, int srcPos, T[] dst, int len)
  8. arrayCopy(T[] x)
  9. arrayCopyAndAddEntry(T[] base, T extra)