4

Possible Duplicate:
How to cast from List<Double> to double[] in Java?

So far the only method I can get working is to loop through the entire array. I was hoping for something along the lines of:

ArrayList<Double> myArrayList = ... // initiaize
double[] myArray = ... // magic line here
Community
  • 1
  • 1
deltanovember
  • 42,611
  • 64
  • 162
  • 244

2 Answers2

5

Use Google Guava : Doubles.toArray

myArray = Doubles.toArray(myArrayList);
lschin
  • 6,745
  • 2
  • 38
  • 52
2

You cannot go from ArrayList<Double> to double[] with a single call using the sandard Java API. However, take a look at the ArrayUtils class available from Apache's site.

On a side note, if you need to change your ArrayList into a primitive array, perhaps your problem is somewhere else. If you explain further exactly what you are trying to achieve, we could provide you better help than a simple API hack.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214