Java examples for Collection Framework:Array Element
Returns a copy of the specified array.
//package com.java2s; import java.lang.reflect.Array; public class Main { public static void main(String[] argv) throws Exception { Object array = "java2s.com"; System.out.println(copyArray(array)); }//from w w w . java 2 s . co m /** * 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) { if (!array.getClass().isArray()) return array; Class componentType = array.getClass().getComponentType(); int length = Array.getLength(array); Object copy = Array.newInstance(componentType, Array.getLength(array)); for (int ii = 0; ii < length; ii++) { Array.set(copy, ii, Array.get(array, ii)); } return copy; } }