Java Object Array Create toObjectArray(Object array)

Here you can find the source of toObjectArray(Object array)

Description

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

License

Apache License

Parameter

Parameter Description
array the primitive array

Exception

Parameter Description
IllegalArgumentException if the array element type is not primitive

Return

a wrapper array

Declaration

public static Object[] toObjectArray(Object array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//  w w w.  j a v a2s .co  m
    * Convert an array of primitive types to an array of objects.
    * E.g. an <code>int[]</code> would be turned into an <code>Integer[]</code>.
    * @param array the primitive array
    * @return a wrapper array
    * @throws IllegalArgumentException if the array element type is not primitive
    */
    public static Object[] toObjectArray(Object array) {
        if (array instanceof Object[]) {
            return (Object[]) array;
        }

        if (array instanceof int[]) {
            int i;
            int k = ((int[]) array).length;
            Integer[] r = new Integer[k];

            for (i = 0; i < k; i++)
                r[i] = new Integer(((int[]) array)[i]);

            return r;
        }

        if (array instanceof boolean[]) {
            int i;
            int k = ((boolean[]) array).length;
            Boolean[] r = new Boolean[k];

            for (i = 0; i < k; i++)
                r[i] = ((boolean[]) array)[i] ? Boolean.TRUE : Boolean.FALSE;

            return r;
        }

        if (array instanceof byte[]) {
            int i;
            int k = ((byte[]) array).length;
            Byte[] r = new Byte[k];

            for (i = 0; i < k; i++)
                r[i] = new Byte(((byte[]) array)[i]);

            return r;
        }

        if (array instanceof char[]) {
            int i;
            int k = ((char[]) array).length;
            Character[] r = new Character[k];

            for (i = 0; i < k; i++)
                r[i] = new Character(((char[]) array)[i]);

            return r;
        }

        if (array instanceof double[]) {
            int i;
            int k = ((double[]) array).length;
            Double[] r = new Double[k];

            for (i = 0; i < k; i++)
                r[i] = new Double(((double[]) array)[i]);

            return r;
        }

        if (array instanceof float[]) {
            int i;
            int k = ((float[]) array).length;
            Float[] r = new Float[k];

            for (i = 0; i < k; i++)
                r[i] = new Float(((float[]) array)[i]);

            return r;
        }

        if (array instanceof long[]) {
            int i;
            int k = ((long[]) array).length;
            Long[] r = new Long[k];

            for (i = 0; i < k; i++)
                r[i] = new Long(((long[]) array)[i]);

            return r;
        }

        if (array instanceof short[]) {
            int i;
            int k = ((short[]) array).length;
            Short[] r = new Short[k];

            for (i = 0; i < k; i++)
                r[i] = new Short(((short[]) array)[i]);

            return r;
        }

        throw new IllegalArgumentException();
    }
}

Related

  1. toObjectArray(byte[] array)
  2. toObjectArray(char... array)
  3. toObjectArray(int[] array)
  4. toObjectArray(int[] array)
  5. toObjectArray(int[] oldArray)
  6. toObjectArray(String[][] originalarray)
  7. toObjectArray(T... t)
  8. toObjectArray(T[] v)
  9. toObjectArraySquareMatrix(T[][] matrixT)