Java tutorial
//package com.java2s; /** * * Copyright 2008 - 2011 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1.1 */ import java.lang.reflect.Array; public class Main { public static Object copyOf(Object src) { int srcLength = Array.getLength(src); Class<?> srcComponentType = src.getClass().getComponentType(); Object dest = Array.newInstance(srcComponentType, srcLength); if (srcComponentType.isArray()) { for (int i = 0; i < Array.getLength(src); i++) { Array.set(dest, i, copyOf(Array.get(src, i))); } } else { System.arraycopy(src, 0, dest, 0, srcLength); } return dest; } public static int[][] copyOf(int[][] obj) { int size = obj.length; int[][] copy = new int[size][]; for (int i = 0; i < size; i++) { int len = obj[i].length; int[] res = new int[len]; System.arraycopy(obj[i], 0, res, 0, len); copy[i] = res; } return copy; } public static String[] copyOf(String[] obj) { return copyOf(obj, obj.length); } public static String[] copyOf(String[] obj, int newSize) { String tempArr[] = new String[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static int[] copyOf(int[] obj) { return copyOf(obj, obj.length); } public static int[] copyOf(int[] obj, int newSize) { int tempArr[] = new int[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static double[] copyOf(double[] obj) { return copyOf(obj, obj.length); } public static double[] copyOf(double[] obj, int newSize) { double tempArr[] = new double[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static float[] copyOf(float[] obj) { return copyOf(obj, obj.length); } public static float[] copyOf(float[] obj, int newSize) { float tempArr[] = new float[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static byte[] copyOf(byte[] obj) { return copyOf(obj, obj.length); } public static byte[] copyOf(byte[] obj, int newSize) { byte tempArr[] = new byte[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static char[] copyOf(char[] obj) { return copyOf(obj, obj.length); } public static char[] copyOf(char[] obj, int newSize) { char tempArr[] = new char[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static long[] copyOf(long[] obj) { return copyOf(obj, obj.length); } public static long[] copyOf(long[] obj, int newSize) { long tempArr[] = new long[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } public static boolean[] copyOf(boolean[] obj) { return copyOf(obj, obj.length); } public static boolean[] copyOf(boolean[] obj, int newSize) { boolean tempArr[] = new boolean[newSize]; System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize)); return tempArr; } }