How do I convert array of object to array of primitive?

Category: commons.lang, viewed: 18786 time(s).

In the code example below we demonstrate the ArrayUtils.toPrimitive() method to convert an array of Integer object to an array of its primitive type. Besides converting array of Integer objects this method is overloadded to accept other types of object array.

package org.kodejava.example.commons.lang;

import org.apache.commons.lang.ArrayUtils;

public class ObjectArrayToPrimitiveDemo {

    public static void main(String[] args) {
        //
        // An array of integer objects.
        //
        Integer[] integers = {new Integer(1), new Integer(2), new Integer(3),
                              new Integer(5), new Integer(8), new Integer(13),
                              new Integer(21), new Integer(34), new Integer(55)};

        //
        // Convert array of integer objects to a primitive array of type int
        //
        int[] fibbos = ArrayUtils.toPrimitive(integers);
        System.out.println(ArrayUtils.toString(fibbos));
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats