Java tutorial
//package com.java2s; /** * * Copyright 2008 - 2009 * * 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 * @emailGceponline@yahoo.com.cn * @version 0.1.1 */ import java.lang.reflect.Array; public class Main { public static Object[] copyOf(Object[] obj) { return copyOf(obj, obj.length); } public static Object[] copyOf(Object[] obj, int newSize) { return (Object[]) copyOf(obj, newSize, ((Object) (obj)).getClass()); } private static Object[] copyOf(Object[] obj, int newSize, Class<? extends Object> newType) { Object[] copy = ((Object) newType == (Object) Object[].class) ? (Object[]) new Object[newSize] : (Object[]) Array.newInstance(newType.getComponentType(), newSize); return copy; } 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; } }