Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# how to use ArrayList to Array() to get array of doubles

Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
Hi,
I am also facing this problem.
I have a ArrayList of Integer Objects. I want to convert to double array. How to do? Could you please help me understand this.
Example:
ArrayList num has 2,3,4,5 Integer Objects
i want to convert it to following double array , like this-
double darr[ ]={2.0,3.0,4.0,5.0}
Ranch Hand

Joined: Jun 17, 2002
Posts: 44
i dont think there is any one step process to do it.
using following code you can get Integer array

then if required you can use it as

hope it helps
[ July 22, 2004: Message edited by: pinky yadav ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Well there's no real benefit to creating an Integer[] array here - you still need to loop through to convert everything, as shown above. Why not do it all in a single loop?

You can convert an int to double directly after all; no casting required.

"I'm not back." - Bill Harding, Twister
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
Integer[] arr=(Integer[])Bus_Imp.toArray(new Integer[0]);
//double doubleArray[];
for(int i=0; i<arr.length;i++){
double doubleArray[i]=arr[i].intValue();
System.out.println("the value got to in doubleArray is:+doubleArray[i]");
System.out.println("the value got to in arr is:+arr[i]");
}

arraytest.java:123: ']' expected
double doubleArray[i]=arr[i].intValue();
^
1 error
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Originally posted by Shivan Belsare:
Integer[] arr=(Integer[])Bus_Imp.toArray(new Integer[0]);
//double doubleArray[];
for(int i=0; i<arr.length;i++){
double doubleArray[i]=arr[i].intValue();
System.out.println("the value got to in doubleArray is:+doubleArray[i]");
System.out.println("the value got to in arr is:+arr[i]");
}

arraytest.java:123: ']' expected
double doubleArray[i]=arr[i].intValue();
^
1 error

Try this:

SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670