Here you can find the source of toPrimitiveArray(Object[] array)
Parameter | Description |
---|---|
array | the wrapper array |
Parameter | Description |
---|---|
IllegalArgumentException | if the array element type is not a primitive wrapper |
public static Object toPrimitiveArray(Object[] array)
//package com.java2s; //License from project: Apache License public class Main { /**// ww w . j ava2 s . c o m * Convert an array of objects to an array of primitive types. * E.g. an <code>Integer[]</code> would be changed to an <code>int[]</code>. * @param array the wrapper array * @return a primitive array * @throws IllegalArgumentException if the array element type is not a primitive wrapper */ public static Object toPrimitiveArray(Object[] array) { if (array instanceof Integer[]) { int[] r = new int[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Integer) array[i]) == null) ? 0 : ((Integer) array[i]).intValue(); return r; } if (array instanceof Boolean[]) { boolean[] r = new boolean[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Boolean) array[i]) == null) ? false : ((Boolean) array[i]).booleanValue(); return r; } if (array instanceof Byte[]) { byte[] r = new byte[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Byte) array[i]) == null) ? 0 : ((Byte) array[i]).byteValue(); return r; } if (array instanceof Character[]) { char[] r = new char[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Character) array[i]) == null) ? 0 : ((Character) array[i]).charValue(); return r; } if (array instanceof Double[]) { double[] r = new double[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Double) array[i]) == null) ? 0 : ((Double) array[i]).doubleValue(); return r; } if (array instanceof Float[]) { float[] r = new float[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Float) array[i]) == null) ? 0 : ((Float) array[i]).floatValue(); return r; } if (array instanceof Long[]) { long[] r = new long[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Long) array[i]) == null) ? 0 : ((Long) array[i]).longValue(); return r; } if (array instanceof Short[]) { short[] r = new short[array.length]; int i; int k = array.length; for (i = 0; i < k; i++) r[i] = (((Short) array[i]) == null) ? 0 : ((Short) array[i]).shortValue(); return r; } throw new IllegalArgumentException(); } }