-2

I am wanting to convert my double ArrayList to a double array but I am unsure how. Here is my current code listing :

ArrayList<Double> aL= new ArrayList<Double>();  
double x[] = new double[aL.size()];
aL.add(10.0);  
aL.add(5.0);  
aL.add(2.0);
for(int i =0; i < x.length; i++){
//some code
}

But then I am not sure where to go and I am unable to find something on the internet/ in books to give me any insight. Thanks.

jahroy
  • 22,322
  • 9
  • 59
  • 108
sam
  • 21
  • 1
  • 2
  • 5
  • This question shows no effort. Please try consulting the [documentation for List](http://docs.oracle.com/javase/7/docs/api/java/util/List.html). – jahroy Mar 21 '13 at 18:33
  • It's pretty straightforward; you've got everything you need in your code (except the contents of the loop). What problems are you having? – Reinstate Monica -- notmaynard Mar 21 '13 at 18:39
  • Helpful links: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Reinstate Monica -- notmaynard Mar 21 '13 at 18:41
  • Here's [one duplicate](http://stackoverflow.com/q/6018267/778118)... and [another](http://stackoverflow.com/q/6147421/778118)... and [another](http://stackoverflow.com/q/960431/778118)... It's a shame that anybody would bother to answer a question like this. It should be downvoated and closed. – jahroy Mar 21 '13 at 19:04

2 Answers2

-2

You can use Collection API to convert list to array. Try uL.toArray();

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
-4

First, you need to create your ArrayList. Then you need to create your array.

List<Double> aL= new ArrayList<Double>();  
aL.add(10.0D);  
aL.add(5.0D);  
aL.add(2.0D);

double x[] = new double[aL.size()];
for(int i = 0; i < x.length; i++){
     x[i] = al.get(i);
}

You can use an ArrayList method to copy the ArrayList to an array.

List<Double> aL= new ArrayList<Double>();  
aL.add(10.0D);  
aL.add(5.0D);  
aL.add(2.0D);

double x[] = aL.toArray();
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • 7
    This is not going to work you will get a compile time :Type mismatch: cannot convert from Object[] to double[] – GKP Oct 11 '13 at 21:47