Generic method to clone array - Java Collection Framework

Java examples for Collection Framework:Array Clone

Description

Generic method to clone array

Demo Code


//package com.java2s;
import java.lang.reflect.Array;

public class Main {
    public static <T> T[] clone_array(T[] array, Class<T> cls)
            throws Exception {
        T[] copy = (T[]) Array.newInstance(cls, array.length);

        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];/*from  w  w  w .  j a v  a2 s. com*/
        }
        return copy;
    }
}

Related Tutorials