Java Array Copy copyArray(T[] array)

Here you can find the source of copyArray(T[] array)

Description

Copies the given array.

License

GNU General Public License

Parameter

Parameter Description
array The array to copy

Return

A copy of the original array, or null if array is null

Declaration

public static <T> T[] copyArray(T[] array) 

Method Source Code

//package com.java2s;
// License: GPL. For details, see LICENSE file.

import java.util.Arrays;

public class Main {
    /**/*from w  w  w. j a v a  2 s .  com*/
     * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
     * @param array The array to copy
     * @return A copy of the original array, or {@code null} if {@code array} is null
     * @since 6221
     */
    public static <T> T[] copyArray(T[] array) {
        if (array != null) {
            return Arrays.copyOf(array, array.length);
        }
        return null;
    }

    /**
     * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
     * @param array The array to copy
     * @return A copy of the original array, or {@code null} if {@code array} is null
     * @since 6222
     */
    public static char[] copyArray(char[] array) {
        if (array != null) {
            return Arrays.copyOf(array, array.length);
        }
        return null;
    }
}

Related

  1. copyArray(Object[] array)
  2. copyArray(Object[] array, int from, int to)
  3. copyArray(Object[] from, Object[] to)
  4. copyArray(String[] array)
  5. copyArray(String[] originalArray)
  6. copyArray(T[] original)
  7. copyArray2(Object v, int len)
  8. copyArrayAddFirst(String[] arr, String add)
  9. copyArrayCutFirst(String[] arr)