集册 Java实例教程 返回指定数组的副本。

返回指定数组的副本。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

572
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回指定数组的副本。


//package com.nowjava;
/* from 
时 代 J a v a 公 众 号 - N o w J a v  a . c o m*/

import java.lang.reflect.Array;


public class Main {

    public static void main(String[] argv) throws Exception {

        Object array = "nowjava.com";

        System.out.println(copyArray(array));

    }


    /**

     * Returns a copy of the specified array. If <i>array</i>

     * is not an array, the object itself will be returned.

     * Otherwise a copy of the array will be returned. The components

     * themselves are not cloned.

     * @param array the array

     * @return the copy of the array

     */

    public static Object copyArray(Object array) {
    /**
     * 时 代 J a v a 公 众 号 - nowjava.com 提 供 
    **/

        if (!array.getClass().isArray())

            return array;

        Class componentType = array.getClass().getComponentType();

        int length = Array.getLength(array);

        Object copy = Array.newInstance(componentType,

                
展开阅读全文