here is what i have got

ArrayList<Integer> list = new ArrayList<Integer>();
    Integer a = 50; 
    Integer b = 55;
    Integer c = 98;
    Integer d = 101;
    list.add(a);
    list.add(b);
    list.add(c);
    list.add(d);

now i want to convert this "list" to an Array... e.g.:

Integer[] actual= {50,55,98,101};

anyway how to do it? thanks.

share|improve this question

67% accept rate
list.toArray(new Integer[0]); – david van brink Nov 21 '11 at 22:11
feedback

2 Answers

up vote 6 down vote accepted
Integer[] array = list.toArray(new Integer[list.size()]);

If you want an int[] array, you'll have to loop over the list and unbox each element explicitly.

See http://download.oracle.com/javase/6/docs/api/java/util/List.html next time you're looking for a method of List.

share|improve this answer
Apache Commons Lang's ArrayUtils.toPrimitive methods can help converting between arrays of wrappers and arrays of primitives. – prunge Nov 21 '11 at 22:46
problem solved, thank you very much – sefirosu Nov 21 '11 at 23:35
feedback

Sefirosu, for another solution you can do it using Arrays.copyOf() or Arrays.copyOfRange() too.

Integer[] integerArray = Arrays.copyOf(list.toArray(), list.toArray().length, Integer[].class);
Integer[] integerArray = Arrays.copyOfRange(list.toArray(), 0, 4, Integer[].class);
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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