Method to convert a Array Collection of int to a Array Collection of Integer. - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Method to convert a Array Collection of int to a Array Collection of Integer.

Demo Code


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        Object objArray = "book2s.com";
        System.out.println(java.util.Arrays.toString(toObject(objArray)));
    }/*from  w  w w  .  jav  a2 s  .co m*/

    /**
     * Method to convert a Array Collection of int to a Array Collection of Integer.
     * @param <T> the generic variable.
     * @param objArray the Array Collection of int.
     * @return Array Collection of Integer.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] toObject(Object... objArray) {
        //return org.apache.commons.lang3.ArrayUtils.toObject(intArray);
        if (objArray == null)
            return null;
        else if (objArray[0].getClass() == int.class) {
            if (objArray.length == 0)
                return (T[]) new Integer[0];
            final Integer[] result = new Integer[objArray.length];
            for (int i = 0; i < objArray.length; i++) {
                result[i] = (Integer) objArray[i];
            }
            return (T[]) result;
        } else
            return null;
    }
}

Related Tutorials