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
  • 40,595
  • 61
  • 158
  • 240

2 Answers2

5

Use Google Guava : Doubles.toArray

myArray = Doubles.toArray(myArrayList);
lschin
  • 6,677
  • 2
  • 37
  • 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
  • 47,885
  • 24
  • 119
  • 191