Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
How to convert an ArrayList containing Integers to primitive int array?

I tried the code below, but it does not work. Do I have to make a loop copy the ArrayList into an array ?

int[]intArray = (int[]) integerArrayList.toArray();
share|improve this question

marked as duplicate by Baz, assylias, Vulcan, Vikdor, Rohit Jain Oct 27 '12 at 10:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers 5

up vote 8 down vote accepted

If you want primitive type array, you can use Apache Commons ArrayUtils#toPrimitive method to get primitive array from wrapper array: -

public static int[] toPrimitive(Integer[] array)

You can use it like this: -

int[] arr = ArrayUtils.toPrimitive((Integer[])integerArrayList.toArray());

Or, you can use the 1-arg version of toArray method, that takes an array and returns the array of that type only. That way, you won't have to the typecasting.

List<Integer> myList = new ArrayList<Integer>();
Integer[] wrapperArr = myList.toArray(new Integer[myList.size()]);

// If you want a `primitive` type array
int[] arr = ArrayUtils.toPrimitive(wrapperArr);

However, if you are Ok with the wrapper type array (Which certainly will not trouble you), then you don't need to do that last step.


Also, you can also create a primitive array, without having to create that intermediate Wrapper type array. For that you would have to write it through loop: -

int[] arr = new int[myList.size()];

for(int i = 0; i < myList.size(); i++) {
    if (myList.get(i) != null) {
        arr[i] = myList.get(i);
    }
}
share|improve this answer
    
It does the same. Loop over array and create new primitive array. And also adding another library for such a simple functionality does not make sense. –  Amit Deshpande Oct 27 '12 at 9:50
    
@AmitD. At least saves us from writing that loop, to get the work done. –  Rohit Jain Oct 27 '12 at 9:51

You'll need to iterate through and copy it.

int[] intArray = new int[integerArrayList.size()];
for (int i = 0; i < intArray.length; i++) {
    intArray[i] = integerArrayList.get(i);
}
share|improve this answer

you should be using arraylist's toArray(T[] arr) method instead of arrayList's toArray() with no args. refer to that method signature here.

do it like below:

   ArrayList<Integer> al = new ArrayList<>();
        al.add(2);
        al.add(3);
        Integer[] arr = al.toArray(new Integer[al.size()]);
        System.out.println(arr.length);//returns 2
share|improve this answer
1  
This is incorrect as he is looking to create a primitive int array, not another array of Integer. –  JamesB Oct 27 '12 at 10:42

You don't need the loop, and you don't need typecast to int. Just change the declaration of int to Integer. Other code will use auto unboxing to int elements if required.

Look at autoboxing

// List adapter for primitive int array
public static List<Integer> asList(final int[] a) {
    return new AbstractList<Integer>() {
        public Integer get(int i) { return a[i]; }
        // Throws NullPointerException if val == null
        public Integer set(int i, Integer val) {
            Integer oldVal = a[i];
            a[i] = val;
            return oldVal;
        }
        public int size() { return a.length; }
    };

}

share|improve this answer

You can get the Integer array as follows:

Integer [] intArray = new Integer[integerArrayList.size()];
integerArrayList.toArray(intArray);
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.