1

Possible Duplicate:
How to convert List<Integer> to int[] in Java?

What's the easiest way to convert a List to int[]?

list.toArray(new int[list.size()]) fails on a typecast error (Integer[] is not a int[]).

I could do it manually, I'm asking if there's an nicer way.

public static int[] toIntArray(List<Integer> list) {
  int[] result = new int[list.size()];
  for (int x : list) result.add(x); // assume no nulls
  return result;
}
Community
  • 1
  • 1
ripper234
  • 222,824
  • 274
  • 634
  • 905

0 Answers0