将基元数组转换为对象数组
//package com.nowjava; /*来自 nowjava.com - 时代Java*/ import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { Object value = "nowjava.com"; System.out.println(java.util.Arrays .toString(convertPrimitiveArrayToObjectArray(value))); } public static Object[] convertPrimitiveArrayToObjectArray(Object value) { if (value instanceof int[]) {//来 自 n o w j a v a . c o m return toPrimitiveArray((int[]) value); } else if (value instanceof long[]) { return toPrimitiveArray((long[]) value); } else if (value instanceof short[]) { return toPrimitiveArray((short[]) value); } else if (value instanceof float[]) { return toPrimitiveArray((float[]) value); } else if (value instanceof double[]) { return toPrimitiveArray((double[]) value); } else if (value instanceof char[]) { return toPrimitiveArray((char[]) value); } else if (value instanceof byte[]) { return toPrimitiveArray((byte[]) value); } else if (value instanceof boolean[]) { return toPrimitiveArray((boolean[]) value); } else if (value instanceof Object[]) { return (Object[]) value; } else { throw new RuntimeException("object isn't an array."); } } public static String toString(long[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(int[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(short[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(char[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(byte[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(boolean[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(float[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(double[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); buf.append('['); buf.append(a[0]); for (int i = 1; i < a.length; i++) { buf.append(", "); buf.append(a[i]); } buf.append("]"); return buf.toString(); } public static String toString(Object[] a) { if (a == null) return "null"; if (a.length == 0) return "[]"; StringBuffer buf = new StringBuffer(); for (int i = 0; i < a.length; i++) { if (i == 0) buf.append('['); else buf.append(", "); buf.append(String.valueOf(a[i])); } buf.append("]"); return buf.toString(); } /** * <p> * Outputs an array as a String, treating <code>null</code> as an empty * array. * * @param array * the array to get a toString for, may be <code>null</code> * @return a String representation of the array, '{}' if null array input */ public static String toString(Object array) { if (array == null) { return "[]"; } if (array instanceof long[]) { return toString((long[]) array); } if (array instanceof int[]) { return toString((int[]) array); } if (array instanceof short[]) { return toString((short[]) array); } if (array instanceof char[]) { return toString((char[]) array); } if (array instanceof byte[]) { return toString((byte[]) array); } if (array instanceof boolean[]) { return toString((boolean[]) array); } if (array instanceof float[]) { return toString((float[]) array); } if (array instanceof double[]) { return toString((double[]) array); } if (array instanceof Object[]) { return toString((Object[]) array); } return array.toString(); } private static Object[] toPrimitiveArray(char values[]) { List<Object> objs = new ArrayList<Object>(); for (Character value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(float values[]) { List<Object> objs = new ArrayList<Object>(); for (Float value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(long values[]) { List<Object> objs = new ArrayList<Object>(); for (Long value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(short values[]) { List<Object> objs = new ArrayList<Object>(); for (Short value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(int values[]) { List<Object> objs = new ArrayList<Object>(); for (Integer value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(double values[]) { List<Object> objs = new ArrayList<Object>(); for (Double value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(boolean values[]) { List<Object> objs = new ArrayList<Object>(); for (Boolean value : values) { objs.add(value); } return objs.toArray(); } private static Object[] toPrimitiveArray(byte values[]) { List<Object> objs = new ArrayList<Object>(); for (Byte value : values) { objs.add(value); } return objs.toArray(); } public static Object[] toArray(Object... objects) { if (objects == null || objects.length == 0) { return new Object[0]; } if (objects.length == 1) { Object o = objects[0]; if (o == null) { return new Object[] { null }; } else if (o.getClass().isArray()) { return convertPrimitiveArrayToObjectArray(o); } else if (o instanceof Collection) { Collection coll = (Collection) o; Object[] array = new Object[coll.size()]; int index = 0; for (Object item : coll) { array[index++] = item; } return array;