Best way to convert a collection into an array in java

Total Post:1

 965  View(s)
Rate this:
I am trying to convert ArrayList into integer array, but its throwing CTE, Is it possible to convert directly a collection into an array i.e. is there any  build-in utility class or method for doing this thing only?
  1. Post:397

    Re: Best way to convert a collection into an array in java

    Hi Elizabeth, 

    There is nothing build-in to convert a collection to an array as far as i know.
    You may need to create a method which use Iterator or listIterator(in case of list) and iterate on all the elements of the collection through it and convert it by type casting into prmitive type and assign the value in the array. Pass your collection inside this method. folloe below code :
    public static int[] convertIntegers(List<Integer> integers)
        int[] ret = new int[integers.size()];
        Iterator<Integer> iterator = integers.iterator();
        for (int i = 0; i < ret.length; i++)
            ret[i] =;
        return ret;



Enter your email address here always to be updated. We promise not to spam!