Java Primitive Type Create toPrimitiveArray(Object[] array)

Here you can find the source of toPrimitiveArray(Object[] array)

Description

Convert an array of objects to an array of primitive types.

License

Apache License

Parameter

Parameter Description
array the wrapper array

Exception

Parameter Description
IllegalArgumentException if the array element type is not a primitive wrapper

Return

a primitive array

Declaration

public static Object toPrimitiveArray(Object[] array) 

Method Source Code

//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();
    }
}

Related

  1. toPrimitive(Object value)
  2. toPrimitiveArray(Boolean[] a)
  3. toPrimitiveArray(Byte[] array)
  4. toPrimitiveArray(Byte[] bytes)
  5. toPrimitiveArray(Integer[] array)
  6. toPrimitiveArrayType(Class c)
  7. toPrimitiveBoolean(Object o)
  8. toPrimitiveBoolean(Object o)
  9. toPrimitiveByteArray(Integer[] array)